stanfordnlp/CoreNLP · error · NoSuchElementException

DocumentIterator exhausted.

Error message

DocumentIterator exhausted.

What it means

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.

Solutions

  1. Guard every next() call with hasNext(): while (it.hasNext()) { Annotation a = it.next(); ... }
  2. Create a new AnnotationIterator to restart iteration instead of reusing the exhausted one
  3. Prefer a for-each loop over the iterator, which cannot over-advance

Example fix

// before
Annotation a = it.next(); // may exhaust
// after
if (it.hasNext()) { Annotation a = it.next(); }
Defensive patterns

Strategy: try-catch

Validate before calling

// always check before advancing
while (it.hasNext()) {
  Annotation doc = it.next();
  process(doc);
}

Try / catch

try {
  Annotation doc = it.next();
} catch (NoSuchElementException e) {
  if (e.getMessage().contains("exhausted")) {
    log.info("No more documents");
  } else throw e;
}

Prevention

When it happens

Trigger: Calling next() when hasNext() returns false (nextDoc == null), typically a loop calling next() more times than there are documents, or calling next() unconditionally once.

Common situations: Off-by-one in manual iteration, calling next() without hasNext() guard, reusing an exhausted iterator for a second pass.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/6798f25bd3da5f27. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/pipeline/AnnotationIterator.java:62

            throw new IOException("Unsupported file format: " + filename);
        }
        nextDoc = readNextDocument();
    }

    public AnnotationIterator(String filename, int limit) throws IOException {
        this(filename);
        this.limit = limit;
    }

    @Override
    public boolean hasNext() {
        return nextDoc != null;
    }

    @Override
    public Annotation next() {
        if (nextDoc == null) {
            throw new NoSuchElementException("DocumentIterator exhausted.");
        }
        Annotation curDoc = nextDoc;
        nextDoc = readNextDocument();
        return curDoc;
    }

    public Annotation readJsonDocument(String str) {
        return jsonReader.read(str);
    }

    public Annotation readNextDocument() {
        if (br == null && input == null) {
            return null;
        }
        if (limit > 0 && docCnt >= limit) {
            return null;
        }
        try {

View on GitHub (pinned to 1b7edd19c4)