{"record":{"id":"0510faf8b7d5e49d","repo":"hibernate/hibernate-orm","slug":"no-more-elements","errorCode":null,"errorMessage":"no more elements","messagePattern":"no more elements","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/format/StringJsonDocumentReader.java","lineNumber":144,"sourceCode":"\t\t\t\tif ( currentState == JsonProcessingState.STARTING_ARRAY) {\n\t\t\t\t\tthis.processingStates.push( JsonProcessingState.ARRAY );\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tthrow new IllegalStateException( \"Unexpected JsonProcessingState \" + marker );\n\t\t}\n\t}\n\n\t/**\n\t * Returns the next item.\n\t * @return the item\n\t * @throws NoSuchElementException no more item available\n\t * @throws IllegalStateException not a well-formed JSON string.\n\t */\n\t@Override\n\tpublic JsonDocumentItemType next() {\n\n\t\tif ( !hasNext()) throw new NoSuchElementException(\"no more elements\");\n\n\t\twhile (hasNext()) {\n\t\t\tskipWhiteSpace();\n\t\t\tStringJsonDocumentMarker marker = StringJsonDocumentMarker.markerOf( this.jsonString.charAt( this.position++ ) );\n\t\t\tmoveStateMachine( marker );\n\t\t\tswitch ( marker) {\n\t\t\t\tcase OBJECT_START:\n\t\t\t\t\tresetValueWindow();\n\t\t\t\t\treturn JsonDocumentItemType.OBJECT_START;\n\t\t\t\tcase OBJECT_END:\n\t\t\t\t\tresetValueWindow();\n\t\t\t\t\t//this.processingStates.pop(); // closing an object or a nested one.\n\t\t\t\t\treturn JsonDocumentItemType.OBJECT_END;\n\t\t\t\tcase ARRAY_START:\n\t\t\t\t\tresetValueWindow();\n\t\t\t\t\t//this.processingStates.push( JsonProcessingState.STARTING_ARRAY );\n\t\t\t\t\treturn JsonDocumentItemType.ARRAY_START;\n\t\t\t\tcase ARRAY_END:","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/format/StringJsonDocumentReader.java#L126-L162","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nwhile (true) {\n    JsonDocumentItemType item = reader.next(); // throws NoSuchElementException at end\n    process(item);\n}\n// after\nwhile (reader.hasNext()) {\n    JsonDocumentItemType item = reader.next();\n    process(item);\n}","handlingStrategy":"validation","validationCode":"if (reader.hasNext()) {\n    JsonDocumentItemType item = reader.next();\n    // process item\n} else {\n    // document fully consumed - stop\n}","typeGuard":null,"tryCatchPattern":"try {\n    item = reader.next();\n} catch (java.util.NoSuchElementException e) {\n    // treat as normal end of the JSON document\n    return;\n}","preventionTips":["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"],"tags":["hibernate","json","iterator","streaming-parser"],"backgroundTag":"no-such-element-exception","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}