NoSuchElementException Java Origin: Application & API Last updated 14 Oct 2026

java.util.NoSuchElementException: No value present

Code asked for an element that doesn’t exist — Optional.get() on empty, iterator.next() past the end, Scanner with no input.

Exact message you may see No value present
Seen on: Java

Meaning

“No value present” comes from Optional.get()/orElseThrow() on an empty Optional. Iterators and Scanner throw it when exhausted.

Common causes

  • Optional.get() without checking
  • iterator.next() without hasNext()
  • Scanner reading beyond input

⚡ Quick fix

  1. Use orElse/orElseThrow with a clear exception
  2. Check hasNext()
  3. Validate input before reading

Detailed fix by platform

Java

  1. User u = repo.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));

How to diagnose

  1. Source — Optional, iterator or Scanner?
  2. Data — Why is it empty?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AccessDeniedException — java.nio.file.AccessDeniedException: /path (Java)AccessDeniedExceptionArithmeticException — java.lang.ArithmeticException: / by zero (Java)ArithmeticExceptionIllegalArgumentException — java.lang.IllegalArgumentException (Java)IllegalArgumentExcept…IllegalStateException — java.lang.IllegalStateException (Java)IllegalStateExceptionIndex was out of range — ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection (.NET)Index was out of rangeSequence contains no elements — InvalidOperationException: Sequence contains no elements / more than one element (.NET)Sequence contains no …SIGABRT — Thread 1: signal SIGABRT (app crashes with "libc++abi: terminating") (iOS)SIGABRTterminate called after throwing an instance of — C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check (C/C++)terminate called afte…NoSuchEle…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.