English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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 occurred 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 access 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 objects, attempting to get the next element after reaching the end of the object (collection, array, or other objects) will generate NoSuchElementException.
The nextElement() method of Enumeration-VectorHashTables and other collections have a method named elements() that returns an Enumeration (interface) object containing all elements of the collection.
Using this object, elements can be obtained one by one using the nextElement() method.
If this method is called on an empty collection, or if NoSuchElementException is generated after reaching the end of the collection, NoSuchElementException will be generated at runtime.
import java.util.Enumeration; import java.util.Vector; public class EnumExample { public static void main(String args[]) { //Instantiate the vector Vector<Integer> vec = new Vector<Integer>( ); //Fill the vector vec.add(1254); vec.add(4587); //Using enumeration to retrieve elements Enumeration<Integer> en = vec.elements(); System.out.println(en.nextElement()); System.out.println(en.nextElement()); //retrieving the next element after reaching the end System.out.println(en.nextElement()); } }
1254 4587 Exception in thread "main" java.util.NoSuchElementException: Vector Enumeration at java.util.Vector$1.nextElement(Unknown Source) at MyPackage.EnumExample.main(EnumExample.java:18)
The nextElement() and nextToken() methods of StringTokenizer -The StringTokenizer class accepts String and delimiter as parameters in one of its constructors, splitting the given String into several small tokens each time the given delimiter appears.
The nextToken() and nextElement() methods of this type return the next token from the token generator. If these methods are called on an empty Tokenizer object, or if NoSuchElementException is called at the end, 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"; //instantiate StringTokenizer class StringTokenizer tokenizer = new StringTokenizer(str, " "); //print all tokens System.out.println(tokenizer.nextToken()); System.out.println(tokenizer.nextToken()); System.out.println(tokenizer.nextToken()); System.out.println(tokenizer.nextToken()); //retrieving 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)
Iterator's next() method -Java provides Iterator and ListIterator classes to retrieve elements of collection objects. The next() method of Iterator and ListIterator returns the next element of the collection.
If these methods are called on an empty collection or NoSuchElementException is called at the end, a runtime exception will be generated.
Similarly, if this method is called on an empty object or at its declaration position, the ListIterator's previous() method will return the previous element of the collection and generate NoSuchElementException at runtime.
import java.util.ArrayList; import java.util.Iterator; public class NextElementExample{ public static void main(String args[]) { //Instantiate an ArrayList object ArrayList<String> list = new ArrayList<String>(); //Fill the ArrayList list.add("apples"); list.add("mangoes"); list.add("oranges"); //Get the Iterator object of ArrayList Iterator it = list.iterator(); System.out.println(it.next()); System.out.println(it.next()); System.out.println(it.next()); //retrieving the next element after reaching the end it.next(); } }
apples mangoes oranges Exception in thread "main" java.util.NoSuchElementException at java.util.ArrayList$Itr.next(Unknown Source) at MyPackage.NextElementExample.main(NextElementExample.java:19)
import java.util.ArrayList; import java.util.ListIterator; public class NextElementExample{ public static void main(String args[]) { //Instantiate an ArrayList object ArrayList<String> list = new ArrayList<String>(); //Fill the ArrayList list.add("apples"); list.add("mangoes"); list.add("oranges"); //Get the Iterator object of ArrayList ListIterator<String> it = list.listIterator(); it.next(); it.next(); it.next(); System.out.println(it.previous()); System.out.println(it.previous()); System.out.println(it.previous()); System.out.println(it.previous()); } }
Output Result
oranges mangoes apples Exception in thread "main" java.util.NoSuchElementException at java.util.ArrayList$ListItr.previous(Unknown Source) at MyPackage.NextElementExample.main(NextElementExample.java:22)