English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
What is the cause of NoSuchElementException, and how to fix it in Java?
An exception is a problem that occurs during program execution (runtime error). When an exception occurs, the program will terminate abruptly, and the code after the line where the exception was generated will never be executed. Each exception is represented by its own class.
This is a runtime exception, which occurs during execution.
If you try to get an element from an empty object, or use the accessor methods of Enumeration, Iterator, or tokenizer (such as next() or nextElement()) to access the content of a collection, array, or other object, a NoSuchElementException will be generated when trying to get the next element after reaching the end of the object (collection, array, or other object).
For example,
If the nextElement() method of the Enumeration class is called on an empty enumeration object, or if the current position is at the end of the Enumeration, a NosuchElementException will be generated at runtime.
If the nextElement() and nextToken() methods of the StringTokenizer class are used on an empty StringTokenizer object, or if the current position is at the end of the StringTokenizer, a NosuchElementException will be generated at runtime.
If the next() method of the Iterator or ListIterator class is called on an empty Iterator / on, or if the current position is at the end, an Iterator will be generated at runtime / ListIterator NosuchElementException.
Similarly, if the previous() method of the ListIterator class is called on an empty ListIterator object, or if the current position is the start of the ListIterator, a NoSuchElementException will be generated at runtime.
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)
Almost all classes that cause NoSuchElementException in their accessor methods include their own methods to verify whether the object (collection, tokenizer, etc.) contains more elements.
For example-
The Enumeration class includes a method named hasMoreElements(), which returns true if the current object contains more elements after the current position (otherwise returns false).
The StringTokenizer class includes methods named hasMoreTokens() and hasMoreElements(), which return true if the current object contains more elements after the current position (otherwise returns false).
The Iterator class includes the hasNext() method, which returns true if the current iterator contains more elements next to the current position (otherwise returns false).
The ListIterator class includes the hasPrevious() method, which returns true if the current iterator contains more elements before the current position (otherwise returns false).
In the while loop, use these methods to verify whether the corresponding object contains more elements, and only print when the condition is true./Access the elements. This can prevent accessing elements using accessor methods when there are no elements in the object, or when reaching the end.
The hasMoreElements() method of the Enumeration class
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()); } } }
Output Result
1254 4587
The nextMoreTokens() method of the StringTokenizer class-
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()); } } }
Output Result
Hello how are you
The hasNext() method of the Iterator class-
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()); } } }
Output Result
apples mangoes oranges
ListIterator class hasPrevious() method-
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()); } } }
Output Result
oranges mangoes apples