Fix Exception in thread main java.util.NoSuchElementException

People are currently reading this guide.
Getting "Exception in thread main java.util.NoSuchElementException" on console? Want get rid of java.util.NoSuchElementException in 2 minutes? Go ahead and find your fix and understand what does this exception mean and what actually caused this error.

In this Java tutorial, we will find the cause of the NoSuchElementException exception in Java and how to avoid it altogether.

Exception in thread main java.util.NoSuchElementException

What is java.util.NoSuchElementException?

Java.util.NoSuchElementException is an exception to RuntimeException that can be generated by different Java classes. Main classes that throw this errors are Scanners, Tokenizers, Enums and Iterators.

This exception extends the RuntimeException class and, as such, includes exceptions that can be thrown during the operation of the Java Virtual Machine (JVM).  If you are also looking for a solution to NPE then Fix for NullPointerException is here.

These are exceptions that are not handled and, therefore, do not need to specify them in the method or release the constructor.

Where NoSuchElementException lies in Java:
  • java.lang.Object
    • Throwable
      • Exception
        • RuntimeException
          • NoSuchElementException

Root Cause of NoSuchElementException

All of these iterator classes have methods for retrieving the next element. The most common example of this iteration in a hash maps. Many of us access elements in hash maps and hash tables without checking if element even exists. And when this happens, the error is thrown.

According to Javadoc, NoSuchElementException is thrown if you call the Enumeration nextElement() method and that
more articles on the list. the following code will delete java.util.NoSuchElementException because of the enumeration of table is empty.

With Enumerator

public class NoSuchElementExceptionExample{
    public static void main(String args[]) {
        Hashtable myMap = new Hashtable();
        Enumeration enum = myMap.elements();
        enum.nextElement();
    }
}

With Iterator.

public class NoSuchElementExceptionExample {
    public static void main(String args[]) {
        HashMap myMap = new HashMap();
        Iterator iterator = myMap.keySet().iterator();
        iterator.next();
    }
}       

While using Scanners

while(!file.next().equals(treasure)) {
   file.next();
}

How to fix java.util.NoSuchElementException

To avoid this Exception in thread main java.util.NoSuchElementException, always check for Iterator.hasNext () or Enumeration.hasMoreElements () before calling the method next () or nextElement ().

Java.util.StringTokenizer can also throw an exception NoSuchElementException if there are no tokens or elements and you call
The nextToken () or nextElement () method. Following is the example java.util.NoSuchElementException when using StringTokenizer in Java

While using Scanners

while(scanner.hasNextLine()) {
    str=scanner.nextLine();
}

Use UTF-8 Encoding

Scanner scannerObject = new Scanner(new FileInputStream(filePathObject), "UTF-8");

Don't use do-while loop

do {
   // your code to access next element
} while (iterator.hasNext());

while (iterator.hasNext()) {
    // Your code here to access next element
}

For more information you can read Oracle Documentation for this exception. 

Any Issues? - Live Connect

You have our undying gratitude for your visit!