java.util.NoSuchElementException
is a RuntimeException or say an UncheckedException and therefore it does not need to be declared in a constructor’s or a method’s throw block.
Table of Contents
java.util.NoSuchElementException
is usually thrown when we try to access some element which is not present or reserved for the underlying data structure in the heap, and, thrown by the different classes, which have method to fetch the next element or next tokens, like Iterator :: next(), Enumeration :: nextElement() , StringTokenizer :: nextToken().Example
When we are iterating over hashmap without implementing the condition to check if there is any element or not, this exception is raised or thrown by Java, now this is the reason we use hasNext() before calling next() on Iterator.
There can be multiple scenarios for java.util.NoSuchElementException. Let’s understand each with the help of examples.
Scenario 1: In case of Enumeration
In Enumeration, if we call the method nextElement() and there is no element present in enumeration then this exception is raised, refer the code below.
Syntax :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.*; class Main { public static void main(String args[]) { Hashtable hmap = new Hashtable(); Enumeration enumer = hmap.elements(); enumer.nextElement(); // java.util.NoSuchElementExcepiton // because the enumeration is empty } } |
Output:
at java.util.Collections$EmptyEnumeration.nextElement(Collections.j ava:4270)
at Main.main(Main.java:7)
Solution
By using a condition to check if it really have elements by calling method hasMoreElements()
.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import java.util.*; class Main { public static void main(String args[]) { Hashtable hmap = new Hashtable(); Enumeration enumer = hmap.elements(); if (enumer.hasMoreElements()) { //java.util.NoSuchElementExcepiton handled by checking enumer.nextElement(); } } } |
Above program will run perfectly fine with no exceptions.
Scenario 2: In case of Iterator
In iterator, if we call the method next() and there is no element present in iterator then this exception is raised, refer the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import java.util.*; class Main { public static void main(String args[]) { HashMap hMap = new HashMap(); Iterator itr = hMap.keySet().iterator(); itr.next(); //java.util.NoSuchElementException here because iterator is //empty } } |
Output:
java.util.HashMap$HashIterator.nextNode(HashMap.java:1447) at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
at Main.main(Main.java:6)
Solution
By implementing a condition to check if there is any next element by calling method hasNext().
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.*; class Main { public static void main(String args[]) { HashMap hMap = new HashMap(); Iterator itr = hMap.keySet().iterator(); if (itr.hasNext()) itr.next(); // java.util.NoSuchElementExcepiton handled } } |
Now, this runs without throwing the given Exception.
Scenario 3: In case of StringTokenizer
In StringTokenizer, if we call the method nextElement() or nextToken() and there is no element or token present then this exception is raised, refer the code below.
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; import java.util.StringTokenizer; class Main { public static void main(String args[]) { StringTokenizer token = new StringTokenizer("", ":"); System.out.println(token.nextToken()); } } |
Output:
at java.util.StringTokenizer.nextToken (StringTokenizer.java:349)
at Main.main (Main.java:5)
Solution
By using Stringtokenizer’s hasMoreTokens() or hashMoreElements()
before proceding to call nextToken() or nextElement().
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.StringTokenizer; class Main { public static void main(String args[]) { StringTokenizer token = new StringTokenizer("", ":"); while (token.hasMoreTokens()) // Exception Handled System.out.println(token.nextToken()); } } |
Above program will run perfectly fine with no exceptions.