简体   繁体   English

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

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

i have a file that consist like this 我有一个像这样的文件

/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

and here is my StringTokenizer code: 这是我的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();
        }

i actually don't have any idea what is the problem as well as i researched every reason why nosuchelementexception would happened but i am not doing all those reasons why it won't work. 我实际上不知道问题出在哪里,我也研究了不会发生nosuchelementexception所有原因,但我并没有做所有nosuchelementexception原因。 any suggestions would be open thank you so much :) 任何建议都会公开,非常感谢:)

    while(read.hasNext()){

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

From the above, Scanner read does not have any tokenizer, hence, read.hasNext() throws NoSuchElementEXception read.hasNextLine() should work fine 从上面的内容来看,Scanner read没有任何标记器,因此read.hasNext()抛出NoSuchElementEXception read.hasNextLine()应该可以正常工作

Exception java.util.NoSuchElementException This exception is thrown to indicate that there are no more elements in an tokenizer. 异常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);
    }

In your while loop you have condition for checking more elements using hasMoreElements() . while循环中,您具有使用hasMoreElements()检查更多元素的条件。 But inside while loop you have for loop which calls nextElement() multiple times. 但是在while循环内部,您有for循环,该循环多次调用nextElement() If there is no elements in tokenizer then nextElement() will throw java.util.NoSuchElementException . 如果tokenizer中没有元素,则nextElement()将抛出java.util.NoSuchElementException

Solution :Add some condition in for loop for checking elements in tokenizer. 解决方案 :在for循环中添加一些条件,以检查标记器中的元素。

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

相关问题 线程“主”中的异常java.util.NoSuchElementException? - Exception in thread “main” java.util.NoSuchElementException? 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 主线程java.util.NoSuchElementException中的异常 - Exception in main thread java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException - Exception in thread "main" java.util.NoSuchElementException Java扫描器错误:线程“主”中的异常java.util.NoSuchElementException - Java Scanner Error: Exception in thread “main” java.util.NoSuchElementException Java中线程“main”java.util.NoSuchElementException中的异常 - Exception in thread "main" java.util.NoSuchElementException in Java java 新手 - 线程“main”中的异常 java.util.NoSuchElementException - New to java - Exception in thread “main” java.util.NoSuchElementException 线程“主”中的Java异常java.util.NoSuchElementException运行时错误 - Java Exception in thread “main” java.util.NoSuchElementException runtime error Java错误-“线程“ main”中的异常” java.util.NoSuchElementException - Java Error - “Exception in thread ”main" java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException,Java 扫描器 - Exception in thread "main" java.util.NoSuchElementException, Java scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM