stanfordnlp/CoreNLP · error · IndexOutOfBoundsException
Error -- tree does not contain
Error message
Error -- tree does not contain
What it means
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.
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.
Example fix
// before
Tree node = tree.getNodeNumber(target);
// after
if (target <= tree.size()) { Tree node = tree.getNodeNumber(target); } Defensive patterns
Strategy: try-catch
Validate before calling
if (target < 1 || target > tree.size()) return null;
Try / catch
try { return tree.getNodeNumber(target); } catch (IndexOutOfBoundsException e) { return null; } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- t insert tree after the
- ancestor: height cannot be negative
- Bad tree state:
- Can only operate on preterminals
- Can't set Tree labels
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/392d94f3600e72cc.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Tree.java:2844
/**
* Fetches the {@code i}th node in the tree, with node numbers defined
* as in {@link #nodeNumber(Tree)}.
*
* @param i the node number to fetch
* @return the {@code i}th node in the tree
* @throws IndexOutOfBoundsException if {@code i} is not between 1 and
* the number of nodes (inclusive) contained in {@code this}.
*/
public Tree getNodeNumber(int i) {
return getNodeNumberHelper(new MutableInteger(1),i);
}
private Tree getNodeNumberHelper(MutableInteger i, int target) {
int i1 = i.intValue();
if(i1 == target)
return this;
if(i1 > target)
throw new IndexOutOfBoundsException("Error -- tree does not contain " + i + " nodes.");
i.incValue(1);
for (Tree kid : children()) {
Tree temp = kid.getNodeNumberHelper(i, target);
if(temp != null)
return temp;
}
return null;
}
/**
* Assign sequential integer indices to the leaves of the tree
* rooted at this {@code Tree}, starting with 1.
* The leaves are traversed from left
* to right. If the node is already indexed, then it uses the existing index.
* This will only work if the leaves extend CoreMap.
*/
public void indexLeaves() {
indexLeaves(1, false);View on GitHub (pinned to 1b7edd19c4)