{"record":{"id":"392d94f3600e72cc","repo":"stanfordnlp/CoreNLP","slug":"error-tree-does-not-contain","errorCode":null,"errorMessage":"Error -- tree does not contain ","messagePattern":"Error -- tree does not contain ","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":2844,"sourceCode":"  /**\n   * Fetches the {@code i}th node in the tree, with node numbers defined\n   * as in {@link #nodeNumber(Tree)}.\n   *\n   * @param i the node number to fetch\n   * @return the {@code i}th node in the tree\n   * @throws IndexOutOfBoundsException if {@code i} is not between 1 and\n   *    the number of nodes (inclusive) contained in {@code this}.\n   */\n  public Tree getNodeNumber(int i) {\n    return getNodeNumberHelper(new MutableInteger(1),i);\n  }\n\n  private Tree getNodeNumberHelper(MutableInteger i, int target) {\n    int i1 = i.intValue();\n    if(i1 == target)\n      return this;\n    if(i1 > target)\n      throw new IndexOutOfBoundsException(\"Error -- tree does not contain \" + i + \" nodes.\");\n    i.incValue(1);\n    for (Tree kid : children()) {\n      Tree temp = kid.getNodeNumberHelper(i, target);\n      if(temp != null)\n        return temp;\n    }\n    return null;\n  }\n\n  /**\n   * Assign sequential integer indices to the leaves of the tree\n   * rooted at this {@code Tree}, starting with 1.\n   * The leaves are traversed from left\n   * to right. If the node is already indexed, then it uses the existing index.\n   * This will only work if the leaves extend CoreMap.\n   */\n  public void indexLeaves() {\n    indexLeaves(1, false);","sourceCodeStart":2826,"sourceCodeEnd":2862,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L2826-L2862","documentation":"Thrown by the private getNodeNumberHelper while searching for the target-th node (1-based pre-order numbering). If the running counter exceeds the target before it is found, the tree has fewer nodes than the requested index, so the node cannot exist.","triggerScenarios":"Calling tree.getNodeNumber(target) (or treeNumber operations) with a target index larger than the total number of nodes in the tree, typically after the tree was pruned or edited.","commonSituations":"Storing node numbers/indices from a previous version of a tree and reusing them after tree modification; converting between different tree sizes in parser output post-processing.","solutions":["Validate target <= tree.size() (or tree.yield-based node count) before calling.","Re-derive node references from the current tree instead of cached node numbers.","Catch IndexOutOfBoundsException and treat the node as absent."],"exampleFix":"// before\nTree node = tree.getNodeNumber(target);\n// after\nif (target <= tree.size()) { Tree node = tree.getNodeNumber(target); }","handlingStrategy":"try-catch","validationCode":"if (target < 1 || target > tree.size()) return null;","typeGuard":null,"tryCatchPattern":"try { return tree.getNodeNumber(target); } catch (IndexOutOfBoundsException e) { return null; }","preventionTips":["Do not cache node numbers across tree edits.","Re-derive targets from the live tree each time.","Check target against the tree's node count first."],"tags":["trees","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}