. Advertisement .
..3..
. Advertisement .
..4..
Accessing an iterable exceeding the maximum limit will result in the NoSuchElementException. This exception notifies that there are no elements available to iterate. Here is an example of this case:
import java.util.*;
public class sample {
public static void main(String[] args) {
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add(new String("One"));
Iterator itr = arrlist.iterator();
System.out.println(itr.next()); // Iterator has one element
System.out.println(itr.next()); // Iterator is "empty"
}
}
Output:
One
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(Unknown Source)
at sample.main(sample.java:10)
Why Does This Problem Occur?
There are various factors resulting in the NoSuchElementException. They are iterator::next(), Enumeration::nextElement(), StringTokenizer::nextElement().
The code below will show you common mistakes leading to this issue:
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
public class main {
public static void main(String[] arguments) {
Set hshSet = new HashSet();
Hashtable hshTble = new Hashtable();
// commands that throw exception.
hshSet.iterator().next();
hshTble.elements().nextElement();
}
}
Output:
Exception in thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1447)
at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
at main.main(main.java:11)
How To Fix: Exception in Thread “Main” Java.util.nosuchelementexception
Method 1: Use Iterator.hasNext()
Input:
// Java program to remove the occurrence of
// NoSuchElementException by using hasNext()
// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;
// driver class
class Geek {
// main method
public static void main(String[] args)
{
// creating an hashmap object
HashMap<Integer, Integer> map = new HashMap<>();
// creating an iterator
Iterator itr = map.keySet().iterator();
// checking the map object using .hasNext()
// method if it has elements to access
// or not before accessing the map using
// .next() method
while (itr.hasNext())
System.out.println(itr.next());
}
}
Output:
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$ javac Geek.java
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$ javac Geek
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$
Method 2: Use Enumeration.hasMoreElements()
Input:
// Java program to remove the occurrence of
// NoSuchElementException by using hasMoreElements();
// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;
// driver class
class Geek {
// main method
public static void main(String[] args)
{
// creating an vector object
Vector<Integer> v = new Vector<>();
// creating an enumerator
Enumeration enumerator = v.elements();
// Checking the vector object using
// hasMorelements method if it has elements
// to access or not before accessing the vector
// using .nextElement() method
while (enumerator.hasMoreElements())
System.out.println(enumerator.nextElement());
}
}
Output:
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$ javac Geek.java
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$ javac Geek
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$
Method 3: Use hasMoreToken()
Input:
import java.util.*;
public class sample {
public static void main(String[] args) {
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add(new String("hello"));
Iterator itr = arrlist.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
Output:
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$ javac Geek.java
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$ javac Geek
computer@DESKTOP-L02HJEQ MINGW64 ~/desktop
$
Conclusion
Exception in Thread “Main” Java.util.nosuchelementexception is such an unwanted and unexpected event disturbing your program’s normal flow. Check the above solutions to get it back to working properly.
Leave a comment