How To Solve Java.Util.Nosuchelementexception Inwards Java
How to ready java.util.NoSuchElementException inward Java
java.util.NoSuchElementException is a RuntimeException which tin sack endure thrown past times unlike classes inward Java similar Iterator, Enumerator, Scanner or StringTokenizer. All of those classes has method to fetch adjacent chemical cistron or adjacent tokens if underlying data-structure doesn't convey whatever chemical cistron Java throws "java.util.NoSuchElementException". Most mutual instance of this iterating over hashmap without checking if in that place is whatever chemical cistron or non as well as that's why it's advised to role hashNext() earlier calling next() on Iterator. In this Java tutorial nosotros volition what causes NoSuchElementException inward Java as well as how to avoid it completely.
Cause of Exception inward thread "main" java.util.NoSuchElementException:
1) As per Javadoc NoSuchElementException is thrown if y'all telephone telephone nextElement() method of Enumeration as well as in that place is
no to a greater extent than chemical cistron inward Enumeration. below code volition throw java.util.NoSuchElementException because Enumeration of hastable is empty.
public flat NoSuchElementExceptionDemo{
populace static void main(String args[]) {
Hashtable sampleMap = novel Hashtable();
Enumeration enumeration = sampleMap.elements();
enumeration.nextElement(); //java.util.NoSuchElementExcepiton hither because enumeration is empty
}
}
Output:
Exception inward thread "main" java.util.NoSuchElementException: Hashtable Enumerator
at java.util.Hashtable$EmptyEnumerator.nextElement(Hashtable.java:1084)
at test.ExceptionTest.main(NoSuchElementExceptionDemo.java:23)
Here is to a greater extent than or less other example of java.util.NoSuchElementException which volition endure thrown because nosotros are calling next() method of Iterator which doesn't incorporate whatever element:
public flat NoSuchElementExceptionExample {
populace static void main(String args[]) {
HashMap sampleMap = novel HashMap();
Iterator itr = sampleMap.keySet().iterator();
itr.next(); //java.util.NoSuchElementExcepiton hither because iterator is empty
}
}
Exception inward thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at test.NoSuchElementExceptionExample.main(ExceptionTest.java:22
In lodge to avoid these NoSuchElementException ever telephone telephone Iterator.hasNext() or Enumeration.hasMoreElements() earlier calling next() or nextElement() method.
java.util.StringTokenizer tin sack besides throw NoSuchElementException if in that place is no to a greater extent than token or chemical cistron as well as y'all call
nextToken() or nextElement() method. hither is an instance of java.util.NoSuchElementException piece using StringTokenizer inward Java
import java.util.StringTokenizer;
public flat StringTokenizerDemo {
populace static void main(String args[]) {
StringTokenizer tokenReader = novel StringTokenizer("", ":");
System.out.println(tokenReader.nextToken());
}
}
Exception inward thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at test.ExceptionTest.main(StringTokenizerDemo.java:23)
To larn rid of this exception piece using Stringtokenizer telephone telephone hasMoreTokens() or hashMoreElements() earlier proceding
to telephone telephone nextToken() or nextElement().
here is modified code which volition non throw java.util.NoSuchElementException fifty-fifty if in that place is no to a greater extent than chemical cistron because
its prophylactic guarded shape hashMoreTokens() method which furnish truthful if in that place is to a greater extent than tokens available.
StringTokenizer tokenReader = novel StringTokenizer("", ":");
while (tokenReader.hasMoreTokens()) {
System.out.println(tokenReader.nextToken());
}
We convey seen possible elbow grease of java.lang.NoSuchElementException in Java , It tin sack come upwards piece using Iterator or Enumeration or StringTokenizer. Best agency to ready NoSuchElementException inward coffee is to avoid it past times checking Iterator alongside hashNext(), Enumeration alongside hashMoreElements() as well as StringTokenizer alongside hashMoreTokens().
Further Learning
Complete Java Masterclass
How to solve Invalid Column Index Exception inward Java JDBC
0 Response to "How To Solve Java.Util.Nosuchelementexception Inwards Java"
Post a Comment