繁体   English   中英

线程“主”中的异常java.util.NoSuchElementException

[英]Exception in thread “main” java.util.NoSuchElementException

我有一个像这样的文件

/1/a/a/a/Female/Single/a/a/January/1/1920/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a

这是我的StringTokenizer代码:

public void retrieveRecords(){
        String holdStr="";
        try {
            fileRead=new FileReader(fileNew);
            read=new Scanner(fileRead);

            while(read.hasNext()){
                holdStr+=read.nextLine()+"\n";
            }
            read.close();

            StringTokenizer strToken=new StringTokenizer(holdStr, "/");

            while(strToken.hasMoreElements()){
                rows=new Vector();
                for(int i=0; i<column.length; i++){
                    rows.add(strToken.nextElement());
                }
                ViewTablePanel.tblModel.addRow(rows);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

我实际上不知道问题出在哪里,我也研究了不会发生nosuchelementexception所有原因,但我并没有做所有nosuchelementexception原因。 任何建议都会公开,非常感谢:)

    while(read.hasNext()){

          holdStr+=read.nextLine()+"\n";
     }

从上面的内容来看,Scanner read没有任何标记器,因此read.hasNext()抛出NoSuchElementEXception read.hasNextLine()应该可以正常工作

异常java.util.NoSuchElementException引发此异常以指示令牌生成器中没有其他元素。

    while(strToken.hasMoreElements()){
        rows=new Vector();
        for(int i=0; i<column.length; i++){
            rows.add(strToken.nextElement()); //Exception will throw on this line
        }
        ViewTablePanel.tblModel.addRow(rows);
    }

while循环中,您具有使用hasMoreElements()检查更多元素的条件。 但是在while循环内部,您有for循环,该循环多次调用nextElement() 如果tokenizer中没有元素,则nextElement()将抛出java.util.NoSuchElementException

解决方案 :在for循环中添加一些条件,以检查标记器中的元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM