hibernate/hibernate-orm · error · NoSuchElementException
no more elements
Error message
no more elements
What it means
StringJsonDocumentReader is Hibernate's streaming pull parser for JSON strings, implementing Iterator<JsonDocumentItemType>. next() throws the standard Java NoSuchElementException when it is called after the parser has consumed the whole document (position >= limit). It signals iteration misuse by the caller, not malformed JSON.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/type/format/StringJsonDocumentReader.java:144
if ( currentState == JsonProcessingState.STARTING_ARRAY) {
this.processingStates.push( JsonProcessingState.ARRAY );
}
break;
default:
throw new IllegalStateException( "Unexpected JsonProcessingState " + marker );
}
}
/**
* Returns the next item.
* @return the item
* @throws NoSuchElementException no more item available
* @throws IllegalStateException not a well-formed JSON string.
*/
@Override
public JsonDocumentItemType next() {
if ( !hasNext()) throw new NoSuchElementException("no more elements");
while (hasNext()) {
skipWhiteSpace();
StringJsonDocumentMarker marker = StringJsonDocumentMarker.markerOf( this.jsonString.charAt( this.position++ ) );
moveStateMachine( marker );
switch ( marker) {
case OBJECT_START:
resetValueWindow();
return JsonDocumentItemType.OBJECT_START;
case OBJECT_END:
resetValueWindow();
//this.processingStates.pop(); // closing an object or a nested one.
return JsonDocumentItemType.OBJECT_END;
case ARRAY_START:
resetValueWindow();
//this.processingStates.push( JsonProcessingState.STARTING_ARRAY );
return JsonDocumentItemType.ARRAY_START;
case ARRAY_END:View on GitHub (pinned to fad1729dce)
Solutions
- Guard every call: while (reader.hasNext()) { JsonDocumentItemType item = reader.next(); ... }
- Stop iterating as soon as the item matching the root structure (OBJECT_END/ARRAY_END) is returned instead of draining the reader
- In generic traversal code that cannot track structure, wrap iteration in try/catch NoSuchElementException and treat it as end-of-stream
Example fix
// before
while (true) {
JsonDocumentItemType item = reader.next(); // throws NoSuchElementException at end
process(item);
}
// after
while (reader.hasNext()) {
JsonDocumentItemType item = reader.next();
process(item);
} Defensive patterns
Strategy: validation
Validate before calling
if (reader.hasNext()) {
JsonDocumentItemType item = reader.next();
// process item
} else {
// document fully consumed - stop
} Try / catch
try {
item = reader.next();
} catch (java.util.NoSuchElementException e) {
// treat as normal end of the JSON document
return;
} Prevention
- Always pair next() with a hasNext() check in the same iteration step
- Terminate iteration on the root OBJECT_END/ARRAY_END item instead of draining the reader
- Never assume next() returns null at the end - this Iterator follows the JDK contract and throws
When it happens
Trigger: Calling reader.next() when reader.hasNext() returns false: draining the iterator past the root closing '}' / ']', calling next() a second time after a first loop already exhausted it, or constructing the reader with an empty/whitespace-only string (hasNext() is false immediately because limit == 0 or only blanks remain).
Common situations: Hand-rolled while(true){ reader.next(); } loops that expect null at the end instead of checking hasNext(); off-by-one traversal that calls next() once more after receiving OBJECT_END/ARRAY_END; generic JSON-walking code reused across reader instances.
Related errors
- forEachRemaining
- No more item in JSON document
- Could not instantiate named strategy class [%s]
- Could not find a FormatMapper for the JSON format, which is
- Can't emulate on error clause on CockroachDB
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/0510faf8b7d5e49d.
Report an issue: GitHub.