stanfordnlp/CoreNLP · error · IllegalArgumentException
ancestor: height cannot be negative
Error message
ancestor: height cannot be negative
What it means
Argument-validation guard in Tree.ancestor(int height, Tree root): the height parameter counts how many levels up to walk (0 = this node, 1 = parent, ...), so a negative value has no meaning and is rejected immediately before any traversal begins. It indicates the caller passed height < 0 — the faulty input is the height argument itself, not tree structure.
Solutions
- Clamp the height: if (h < 0) return null; before calling ancestor
- Compute the level difference with depth/pathLength checks and only call ancestor when height >= 0
- Fix the index arithmetic producing the negative height
Example fix
// before Tree anc = node.ancestor(node.depth() - target.depth(), root); // can be negative // after int h = node.depth() - target.depth(); Tree anc = h >= 0 ? node.ancestor(h, root) : null;
Defensive patterns
Strategy: validation
Validate before calling
int h = computedHeight; if (h < 0) return null; Tree anc = node.ancestor(h, root);
Type guard
boolean validAncestorHeight(int h) { return h >= 0; } Try / catch
try { return node.ancestor(h, root); } catch (IllegalArgumentException e) { return null; } Prevention
- Clamp or validate computed heights before calling ancestor
- Remember height 0 returns the node itself
- Verify depth arithmetic sign when comparing node levels
When it happens
Trigger: Calling ancestor(-1, root) or similar, usually from a loop computing relative height as `someIndex - currentIndex` where the index arithmetic can go negative.
Common situations: Off-by-one arithmetic in sentence-segmentation or clause-level extraction code; passing a difference of depths that is negative because the 'ancestor' is actually a descendant.
Related errors
- Can't set Tree labels
- TreeIterator exhausted
- Tree.valueOf() tree construction failed
- Can only operate on preterminals
- : missing tag for
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/8c878f80c22b0ce3.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Tree.java:2282
for (Tree kid : kids) {
size += kid.size();
}
return size;
}
/**
* Return the ancestor tree node {@code height} nodes up from the current node.
*
* @param height How many nodes up to go. A parameter of 0 means return
* this node, 1 means to return the parent node and so on.
* @param root The root node that this Tree is embedded under
* @return The ancestor at height {@code height}. It returns null
* if it does not exist or the tree implementation does not keep track
* of parents
*/
public Tree ancestor(int height, Tree root) {
if (height < 0) {
throw new IllegalArgumentException("ancestor: height cannot be negative");
}
if (height == 0) {
return this;
}
Tree par = parent(root);
if (par == null) {
return null;
}
return par.ancestor(height - 1, root);
}
private static class TreeIterator implements Iterator<Tree> {
private final List<Tree> treeStack;
protected TreeIterator(Tree t) {
treeStack = new ArrayList<>();View on GitHub (pinned to 1b7edd19c4)