English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
IlleagalStateException is generated when the method is called at an illegal or inappropriate time.
For example, the remove() method of the ArrayList class will delete the last element after calling next() or Previous method.
After deleting an element at the current position, you need to move to the next element to delete it, that is, you can only call the remove() method once each time you call the next() method.
Since the initial position of the list (pointer) will be before the first element, this method cannot be called without calling the next method.
If the remove() method is called, otherwise java.lang.IllegalStateException will be thrown.
import java.util.ArrayList; import java.util.ListIterator; public class NextElementExample{ public static void main(String args[]) { //Instantiate the ArrayList object ArrayList<String> list = new ArrayList<String>(); //Fill the ArrayList list.add("apples"); list.add("mangoes"); //Get the Iterator object of ArrayList ListIterator<String> it = list.listIterator(); //Move the element to the first position it.remove(); } }
Exception in thread "main" java.lang.IllegalStateException at java.util.ArrayList$Itr.remove(Unknown Source) at MyPackage.NextElementExample.main(NextElementExample.java17)
import java.util.ArrayList; import java.util.ListIterator; public class NextElementExample{ public static void main(String args[]) { //Instantiate the ArrayList object ArrayList<String> list = new ArrayList<String>(); //Fill the ArrayList list.add("apples"); list.add("mangoes"); //Get the Iterator object of ArrayList ListIterator<String> it = list.listIterator(); //Move the element to the first position it.next(); it.remove(); it.remove(); } }
Output Result
Exception in thread "main" java.lang.IllegalStateException at java.util.ArrayList$Itr.remove(Unknown Source) at MyPackage.NextElementExample.main(NextElementExample.java17)
In this case, also try to call this method in the loop.
it.next(); while(it.hasNext()) { it.remove(); }
In the aforementioned situation, to resolve IllegalStateException, you need to correctly call the remove() method (only once after calling next())
import java.util.ArrayList; import java.util.Arrays; 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"); //Getting the Iterator object of the ArrayList ListIterator<String> it = list.listIterator(); //Removing the element without moving to the first position System.out.println(Arrays.toString(list.toArray())); while(it.hasNext()) { it.next(); it.remove(); } System.out.println(Arrays.toString(list.toArray())); } }
Output Result
[apples, mangoes] []
Similarly, in each case where you need to handle an illegal state exception, you must call the method that causes the exception at its legal position.
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 called 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 the ListIterator, or if the current position is at the end, an Iterator is generated at runtime / listIterator NosuchElementException.
Similarly, if the ListIterator class's previous() method is called on an empty ListIterator object, or if the current position is the beginning of the ListIterator, a NosuchElementException is generated at runtime.
Let's consider a complete example of a situation
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 with their accessor methods include their own methods to verify whether the object (collections, token generators, 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).
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 main difference between these two exceptions is that an IllegalStateException is generated when you call a method at an illegal position in the program.
If you try to access the elements of Enumeration, Iterator, StringTokenizer, etc. (using accessor methods), a NoElementException is generated when there are no more elements.