stanfordnlp/CoreNLP · error · NoSuchElementException

DocumentIterator exhausted.

Error message

DocumentIterator exhausted.

What it means

CoNLL2011DocumentReader.DocumentIterator implements Iterator<Document>; calling next() after the underlying document stream is exhausted throws NoSuchElementException with this message, per the Iterator contract.

Solutions

  1. Always call hasNext() before next()
  2. Restructure loops to use while (reader.hasNext()) { reader.next(); }
  3. Check your expected document count against the actual corpus listing

Example fix

// before
while (true) {
  Document d = docIterator.next(); // throws at end
  process(d);
}
// after
while (docIterator.hasNext()) {
  Document d = docIterator.next();
  process(d);
}
Defensive patterns

Strategy: validation

Validate before calling

if (docIterator.hasNext()) { Document d = docIterator.next(); }

Try / catch

try {
  Document d = docIterator.next();
} catch (NoSuchElementException e) {
  // iterator exhausted; stop iterating
}

Prevention

When it happens

Trigger: Calling DocumentIterator.next() when hasNext() has returned false (nextDoc == null) — i.e. iterating past the last document in the CoNLL file set.

Common situations: Custom training scripts iterating documents without checking hasNext(), miscounting expected documents in a CoNLL corpus, reusing an exhausted reader in dcoref main-style loops.

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


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

Appendix: source

Thrown at src/edu/stanford/nlp/dcoref/CoNLL2011DocumentReader.java:293

    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 Document next() {
      if (nextDoc == null) {
        throw new NoSuchElementException("DocumentIterator exhausted.");
      }
      Document 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] + ")";

View on GitHub (pinned to 1b7edd19c4)