stanfordnlp/CoreNLP · error · NoSuchElementException
DocumentIterator exhausted.
Error message
DocumentIterator exhausted.
What it means
CoNLLDocumentReader.DocumentIterator.next() implements java.util.Iterator semantics: when the internal lookahead document (nextDoc) is null because all underlying files/lines have been consumed, it throws NoSuchElementException instead of returning null. Callers are expected to check hasNext() before calling next(); this throw signals the iterator was advanced past its end.
Solutions
- Guard every next() call with while (it.hasNext()) { ... }
- If you need a reusable pass, create a new DocumentIterator from the reader instead of reusing the exhausted one
- If collecting all documents, use a for-each loop over the Iterable rather than manual next() calls
Example fix
// before
while (true) {
CoNLLDocument doc = docIter.next(); // throws at end
process(doc);
}
// after
while (docIter.hasNext()) {
CoNLLDocument doc = docIter.next();
process(doc);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (docIter.hasNext()) { CoNLLDocument d = docIter.next(); } Type guard
function nextOrNull(Iterator<CoNLLDocument> it) { return it.hasNext() ? it.next() : null; } Try / catch
try { doc = docIter.next(); } catch (NoSuchElementException e) { doc = null; // treat as end of iteration } Prevention
- Always loop with hasNext() before next()
- Never call next() twice for one element
- Create a fresh iterator for a second pass over the corpus
When it happens
Trigger: Calling next() on the reader's DocumentIterator after hasNext() returned false, or calling next() more times than there are documents without checking hasNext().
Common situations: Manual iteration loops over CoNLL corpus files that use a do-while or fixed-count loop instead of while(hasNext()); reusing an exhausted iterator after a first pass; off-by-one when counting documents in the CoNLL-2012 corpus.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- Error running hybrid coref system
- No more elements from
- DocumentIterator exhausted.
- Coreference is not implemented for Spanish
- Processed trees, but there are more trees and text is empty
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/ee1fc979c5a5e38c.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/coref/docreader/CoNLLDocumentReader.java:342
int lineCnt = 0;
int docCnt = 0;
public DocumentIterator(String filename, Options options) throws IOException {
this.options = options;
this.filename = filename;
this.br = IOUtils.readerFromString(filename);
nextDoc = readNextDocument();
}
@Override
public boolean hasNext() {
return nextDoc != null;
}
@Override
public CoNLLDocument next() {
if (nextDoc == null) {
throw new NoSuchElementException("DocumentIterator exhausted.");
}
CoNLLDocument curDoc = nextDoc;
nextDoc = readNextDocument();
return curDoc;
}
private static final Pattern starPattern = Pattern.compile("\\*");
private static Tree wordsToParse(List<String[]> sentWords) {
StringBuilder sb = new StringBuilder();
for (String[] fields:sentWords) {
if (sb.length() > 0) {
sb.append(' ');
}
String str = fields[FIELD_PARSE_BIT].replace("NOPARSE", "X");
String tagword = "(" + fields[FIELD_POS_TAG] + " " + fields[FIELD_WORD] + ")";
// Replace starsView on GitHub (pinned to 1b7edd19c4)