{"record":{"id":"a5d8d0e6da093573","repo":"stanfordnlp/CoreNLP","slug":"treeiterator-exhausted","errorCode":null,"errorMessage":"TreeIterator exhausted","messagePattern":"TreeIterator exhausted","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":2313,"sourceCode":"  private static class TreeIterator implements Iterator<Tree> {\n\n    private final List<Tree> treeStack;\n\n    protected TreeIterator(Tree t) {\n      treeStack = new ArrayList<>();\n      treeStack.add(t);\n    }\n\n    @Override\n    public boolean hasNext() {\n      return (!treeStack.isEmpty());\n    }\n\n    @Override\n    public Tree next() {\n      int lastIndex = treeStack.size() - 1;\n      if (lastIndex < 0) {\n        throw new NoSuchElementException(\"TreeIterator exhausted\");\n      }\n      Tree tr = treeStack.remove(lastIndex);\n      Tree[] kids = tr.children();\n      // so that we can efficiently use one List, we reverse them\n      for (int i = kids.length - 1; i >= 0; i--) {\n        treeStack.add(kids[i]);\n      }\n      return tr;\n    }\n\n    /**\n     * Not supported\n     */\n    @Override\n    public void remove() {\n      throw new UnsupportedOperationException();\n    }\n","sourceCodeStart":2295,"sourceCodeEnd":2331,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L2295-L2331","documentation":"Tree's iterator throws NoSuchElementException with this message when next() is called after the traversal stack is empty. This is the standard Java iterator contract; the message just identifies which iterator was over-advanced.","triggerScenarios":"Calling next() more times than there are nodes — typically a loop that calls next() without hasNext(), or reusing an exhausted iterator.","commonSituations":"Manual while-loops calling it.next() twice per iteration; nested iteration over the same iterator; storing next() results in two variables from a single call site.","solutions":["Guard every next() with iterator.hasNext() (or use an enhanced for-loop over the tree)","Do not reuse an exhausted TreeIterator; obtain a fresh iterator() call","Make sure each iteration consumes exactly one next() call"],"exampleFix":"// before\nwhile (true) { Tree t = it.next(); ... } // exhausts\n// after\nwhile (it.hasNext()) { Tree t = it.next(); ... }","handlingStrategy":"type-guard","validationCode":"if (!it.hasNext()) return null; Tree t = it.next();","typeGuard":"Tree nextOrNull(Iterator<Tree> it) { return it.hasNext() ? it.next() : null; }","tryCatchPattern":"try { Tree t = it.next(); } catch (NoSuchElementException e) { /* iterator exhausted; stop loop */ }","preventionTips":["Always loop on hasNext() or use for (Tree t : tree)","Never call next() twice per loop iteration","Create a fresh iterator per traversal"],"tags":["nlp","trees","iterator","no-such-element"],"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"}