Friday, February 24, 2017

How to solve java.util.NoSuchElementException in Java

How to fix java.util.NoSuchElementException in Java

java.util.NoSuchElementException is a RuntimeException which can be thrown by different classes in Java like Iterator, Enumerator, Scanner or StringTokenizer. All of those classes has method to fetch next element or next tokens if underlying data-structure doesn't have any element Java throws "java.util.NoSuchElementException". Most common example of this iterating over hashmap without checking if there is any element or not and that's why it's advised to use hashNext() before calling next() on Iterator. In this Java tutorial we will what causes NoSuchElementException in Java and how to avoid it completely.

Cause of Exception in thread "main" java.util.NoSuchElementException in Java:

How to solve java.util.NoSuchElementException in Javahere are possible cause of java.util.NoSuchElementException in Java:

1) As per Javadoc NoSuchElementException is thrown if you call the nextElement() method of Enumeration and there is
no more element in Enumeration. below code will throw java.util.NoSuchElementException because the Enumeration of hashtable is empty.

public class NoSuchElementExceptionDemo{

    public static void main(String args[]) {
        Hashtable sampleMap = new Hashtable();
        Enumeration enumeration = sampleMap.elements();
        enumeration.nextElement();  //java.util.NoSuchElementExcepiton here because enumeration is empty

Exception in thread "main" java.util.NoSuchElementException: Hashtable Enumerator
        at java.util.Hashtable$EmptyEnumerator.nextElement(
        at test.ExceptionTest.main(

And, If you are new to Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.

Here is another example of a java.util.NoSuchElementException which will be thrown because we are calling next() method of Iterator which doesn't contain any element:

public class NoSuchElementExceptionExample {

    public static void main(String args[]) {
        HashMap sampleMap = new HashMap();
        Iterator itr = sampleMap.keySet().iterator();;  //java.util.NoSuchElementExcepiton here because iterator is empty

Exception in thread "main" java.util.NoSuchElementException
        at java.util.HashMap$HashIterator.nextEntry(
        at java.util.HashMap$
        at test.NoSuchElementExceptionExample.main(

In order to avoid these NoSuchElementException always call Iterator.hasNext() or Enumeration.hasMoreElements() before calling next() or nextElement() method.

java.util.StringTokenizer can also throw NoSuchElementException if there is no more token or element and you call
nextToken() or nextElement() method. here is an example of java.util.NoSuchElementException while using StringTokenizer in Java

import java.util.StringTokenizer;

public class StringTokenizerDemo {
    public static void main(String args[]) {
        StringTokenizer tokenReader = new StringTokenizer("", ":");

Exception in thread "main" java.util.NoSuchElementException
        at java.util.StringTokenizer.nextToken(
        at test.ExceptionTest.main(

To get rid of this exception while using Stringtokenizer call hasMoreTokens() or hashMoreElements() before proceding
to call nextToken() or nextElement().

here is modified code which will not throw java.util.NoSuchElementException even if there is no more element because
its safe guarded form hashMoreTokens() method which return true if there is more tokens available.

 StringTokenizer tokenReader = new StringTokenizer("", ":");
 while (tokenReader.hasMoreTokens()) {

We have seen possible cause of java.lang.NoSuchElementException in Java , It can come while using Iterator or Enumeration or StringTokenizer. Best way to fix NoSuchElementException in java is to avoid it by checking Iterator with hashNext(), Enumeration with hashMoreElements() and StringTokenizer with hashMoreTokens().

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Some Java Tutorials you may like


Anonymous said...

My programe was throwing "java.util.nosuchelementexception hashtable enumerator" and I was clueless about it. your tutorial helped me.

Anonymous said...

I have check hasNext() Then also this error is coming

Anonymous said...

Most probably you are calling next() method more than "the number of existing elements" inside the loop.


bstpanchkula said...

Thanks . The problem was I was not using hasNext()/hasPrevoius().

Unknown said...

import java.util.*;
import java.lang.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
public static void main (String[] args) throws java.lang.Exception
Scanner sc=new Scanner(;
int t;
int n,k,i;
int count=0;
Long[] a=new Long[(int) n];


}// your code goes here

why i am geting this error how can i get rid of this

javin paul said...

It seems you code is not complete, I see a lot of white space in the end, also cna you post the error, which contains line number that will help

Post a Comment