{"record":{"id":"2b1880efefcb5d3b","repo":"stanfordnlp/CoreNLP","slug":"documentiterator-exhausted-2b1880","errorCode":null,"errorMessage":"DocumentIterator exhausted.","messagePattern":"DocumentIterator exhausted\\.","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/dcoref/CoNLL2011DocumentReader.java","lineNumber":293,"sourceCode":"    int lineCnt = 0;\n    int docCnt = 0;\n\n    public DocumentIterator(String filename, Options options) throws IOException {\n      this.options = options;\n      this.filename = filename;\n      this.br = IOUtils.readerFromString(filename);\n      nextDoc = readNextDocument();\n    }\n\n    @Override\n    public boolean hasNext() {\n      return nextDoc != null;\n    }\n\n    @Override\n    public Document next() {\n      if (nextDoc == null) {\n        throw new NoSuchElementException(\"DocumentIterator exhausted.\");\n      }\n      Document curDoc = nextDoc;\n      nextDoc = readNextDocument();\n      return curDoc;\n    }\n\n    private static final Pattern starPattern = Pattern.compile(\"\\\\*\");\n\n    private static Tree wordsToParse(List<String[]> sentWords)\n    {\n      StringBuilder sb = new StringBuilder();\n      for (String[] fields:sentWords) {\n        if (sb.length() > 0) {\n          sb.append(' ');\n        }\n\n        String str = fields[FIELD_PARSE_BIT].replace(\"NOPARSE\", \"X\");\n        String tagword = \"(\" + fields[FIELD_POS_TAG] + \" \" + fields[FIELD_WORD] + \")\";","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/dcoref/CoNLL2011DocumentReader.java#L275-L311","documentation":"CoNLL2011DocumentReader.DocumentIterator implements Iterator<Document>; calling next() after the underlying document stream is exhausted throws NoSuchElementException with this message, per the Iterator contract.","triggerScenarios":"Calling DocumentIterator.next() when hasNext() has returned false (nextDoc == null) — i.e. iterating past the last document in the CoNLL file set.","commonSituations":"Custom training scripts iterating documents without checking hasNext(), miscounting expected documents in a CoNLL corpus, reusing an exhausted reader in dcoref main-style loops.","solutions":["Always call hasNext() before next()","Restructure loops to use while (reader.hasNext()) { reader.next(); }","Check your expected document count against the actual corpus listing"],"exampleFix":"// before\nwhile (true) {\n  Document d = docIterator.next(); // throws at end\n  process(d);\n}\n// after\nwhile (docIterator.hasNext()) {\n  Document d = docIterator.next();\n  process(d);\n}","handlingStrategy":"validation","validationCode":"if (docIterator.hasNext()) { Document d = docIterator.next(); }","typeGuard":null,"tryCatchPattern":"try {\n  Document d = docIterator.next();\n} catch (NoSuchElementException e) {\n  // iterator exhausted; stop iterating\n}","preventionTips":["Always guard next() with hasNext()","Never reuse an exhausted DocumentIterator","Recreate the reader instead of looping on it past the end"],"tags":["iterator","no-such-element","conll","loop-bug"],"backgroundTag":"empty-result-set","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"}