{"record":{"id":"6798f25bd3da5f27","repo":"stanfordnlp/CoreNLP","slug":"documentiterator-exhausted-6798f2","errorCode":null,"errorMessage":"DocumentIterator exhausted.","messagePattern":"DocumentIterator exhausted\\.","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/pipeline/AnnotationIterator.java","lineNumber":62,"sourceCode":"            throw new IOException(\"Unsupported file format: \" + filename);\n        }\n        nextDoc = readNextDocument();\n    }\n\n    public AnnotationIterator(String filename, int limit) throws IOException {\n        this(filename);\n        this.limit = limit;\n    }\n\n    @Override\n    public boolean hasNext() {\n        return nextDoc != null;\n    }\n\n    @Override\n    public Annotation next() {\n        if (nextDoc == null) {\n            throw new NoSuchElementException(\"DocumentIterator exhausted.\");\n        }\n        Annotation curDoc = nextDoc;\n        nextDoc = readNextDocument();\n        return curDoc;\n    }\n\n    public Annotation readJsonDocument(String str) {\n        return jsonReader.read(str);\n    }\n\n    public Annotation readNextDocument() {\n        if (br == null && input == null) {\n            return null;\n        }\n        if (limit > 0 && docCnt >= limit) {\n            return null;\n        }\n        try {","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/pipeline/AnnotationIterator.java#L44-L80","documentation":"A NoSuchElementException sentinel in AnnotationIterator.next(): called after the underlying document file has been fully consumed (nextDoc == null), i.e. the consumer kept iterating past the last annotation.","triggerScenarios":"Calling next() when hasNext() returns false (nextDoc == null), typically a loop calling next() more times than there are documents, or calling next() unconditionally once.","commonSituations":"Off-by-one in manual iteration, calling next() without hasNext() guard, reusing an exhausted iterator for a second pass.","solutions":["Guard every next() call with hasNext(): while (it.hasNext()) { Annotation a = it.next(); ... }","Create a new AnnotationIterator to restart iteration instead of reusing the exhausted one","Prefer a for-each loop over the iterator, which cannot over-advance"],"exampleFix":"// before\nAnnotation a = it.next(); // may exhaust\n// after\nif (it.hasNext()) { Annotation a = it.next(); }","handlingStrategy":"try-catch","validationCode":"// always check before advancing\nwhile (it.hasNext()) {\n  Annotation doc = it.next();\n  process(doc);\n}","typeGuard":null,"tryCatchPattern":"try {\n  Annotation doc = it.next();\n} catch (NoSuchElementException e) {\n  if (e.getMessage().contains(\"exhausted\")) {\n    log.info(\"No more documents\");\n  } else throw e;\n}","preventionTips":["Guard next() with hasNext()","Use for-each loops over the iterator","Never reuse an exhausted iterator; create a new one"],"tags":["iterator","nosuchelement","pipeline","java"],"backgroundTag":"iterator-exhausted","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}