NoSuchElementException的原因是什么,如何在Java中修复它?
NoSuchElementException的原因是什么,如何在Java中修复它?
例外是程序执行期间发生的问题(运行时错误)。发生异常时,程序会突然终止,并且生成异常的行之后的代码将永远不会执行。每个异常由其各自的类表示。
NosuchElementException的原因
这是运行时异常,即它在执行时发生。
如果您尝试从空对象获取元素,或者使用Enumeration,Iterator或tokenizer的访问器方法(例如next()或nextElement())访问集合,数组或其他对象的内容,则尝试在到达对象(集合,数组或其他对象)的末尾后获取下一个元素,将生成NoSuchElementException。
例如,
如果在空的枚举对象上调用Enumeration类的nextElement()方法,或者如果当前位置在Enumeration的末尾,则将在运行时生成NosuchElementException。
如果在空的StringTokenizer对象上使用StringTokenizer类的nextElement()和nextToken()方法,或者如果当前位置在StringTokenizer的末尾,则在运行时生成NosuchElementException。
如果Iterator或ListIterator类的next()方法在空的Iterator/ListIterator上调用,或者如果当前位置在末尾,则在运行时生成Iterator/listIteratorNosuchElementException。
类似地,如果在空的ListIterator对象上调用ListIterator类的previous()方法,或者如果当前位置是ListIterator的开始,则在运行时会生成NosuchElementException。
示例
import java.util.StringTokenizer; public class StringTokenizerExample{ public static void main(String args[]) { String str = "Hello how are you"; //Instantiating the StringTokenizer class StringTokenizer tokenizer = new StringTokenizer(str, " "); //Printing all the tokens System.out.println(tokenizer.nextToken()); System.out.println(tokenizer.nextToken()); System.out.println(tokenizer.nextToken()); System.out.println(tokenizer.nextToken()); //Getting the next token after reaching the end tokenizer.nextToken(); tokenizer.nextElement(); } }
运行时错误
Hello how are you Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at MyPackage.StringTokenizerExample.main(StringTokenizerExample.java:16)
处理/修复NosuchElementException
几乎所有其访问器方法导致NoSuchElementException的类都包含各自的方法,以验证对象(集合,令牌生成器等)是否包含更多元素。
例如-
Enumeration类包含一个名为hasMoreElements()的方法,如果当前对象在当前位置之后包含更多元素,则该方法返回true(否则返回false)。
StringTokenizer类包含名为hasMoreTokens()和hasMoreElements()的方法,如果当前对象在当前位置之后包含更多元素,则该方法返回true(否则返回false)。
Iterator类包含hasNext()方法,如果当前迭代器在当前位置旁边包含更多元素,则此方法也返回true(否则返回false)。
ListIterator类包含hasPrevious()方法,如果当前迭代器在当前位置之前包含更多元素,则该方法还返回true(否则返回false)。
在while循环中,使用这些方法验证相应对象是否包含更多元素,仅在条件为true时才打印/访问元素。当对象中没有元素时,或者到达末尾时,这可以防止使用访问器方法访问元素。
Enumeration类的hasMoreElements()方法
import java.util.Enumeration; import java.util.Vector; public class EnumExample { public static void main(String args[]) { //instantiating a Vector Vector<Integer> vec = new Vector<Integer>( ); //Populating the vector vec.add(1254); vec.add(4587); //Retrieving the elements using the Enumeration Enumeration<Integer> en = vec.elements(); while(en.hasMoreElements()) { System.out.println(en.nextElement()); } } }
输出结果
1254 4587
StringTokenizer类的nextMoreTokens()方法-
import java.util.StringTokenizer; public class StringTokenizerExample{ public static void main(String args[]) { String str = "Hello how are you"; //Instantiating the StringTokenizer class StringTokenizer tokenizer = new StringTokenizer(str, " "); //Printing all the tokens while(tokenizer.hasMoreTokens()) { System.out.println(tokenizer.nextToken()); } } }
输出结果
Hello how are you
Iterator类的hasNext()方法-
import java.util.ArrayList; import java.util.Iterator; public class NextElementExample{ public static void main(String args[]) { //Instantiating an ArrayList object ArrayList<String> list = new ArrayList<String>(); //populating the ArrayList list.add("apples"); list.add("mangoes"); list.add("oranges"); //Getting the Iterator object of the ArrayList Iterator it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
输出结果
apples mangoes oranges
ListIterator类的hasPrevious()方法-
import java.util.ArrayList; import java.util.ListIterator; public class NextElementExample{ public static void main(String args[]) { //Instantiating an ArrayList object ArrayList<String> list = new ArrayList<String>(); //populating the ArrayList list.add("apples"); list.add("mangoes"); list.add("oranges"); //Getting the Iterator object of the ArrayList ListIterator<String> it = list.listIterator(); while(it.hasNext()) { it.next(); } while(it.hasPrevious()) { System.out.println(it.previous()); } } }
输出结果
oranges mangoes apples