stanfordnlp/CoreNLP · error · IllegalArgumentException
t insert tree after the
Error message
t insert tree after the
What it means
Thrown by Tree.insertDtr(Tree dtr, int position) when the requested insertion position is greater than the current number of daughters. The tree has only kids.length children, so a position beyond that (only position == kids.length, append at end, is legal) is invalid.
Solutions
- Clamp the position: call insertDtr(dtr, Math.min(position, children().length)).
- Verify the daughter count with tree.children().length before inserting.
- Recompute the position against the current tree rather than a cached one.
Example fix
// before tree.insertDtr(newNode, idx); // after tree.insertDtr(newNode, Math.min(idx, tree.children().length));
Defensive patterns
Strategy: validation
Validate before calling
if (position < 0 || position > tree.children().length) throw new IllegalArgumentException("invalid insert position");
tree.insertDtr(dtr, position); Try / catch
try { tree.insertDtr(dtr, position); } catch (IllegalArgumentException e) { /* clamp and retry */ tree.insertDtr(dtr, tree.children().length); } Prevention
- Always check children().length before inserting.
- Remember the API allows appending at position == kids.length only.
- Recompute positions after any tree mutation.
When it happens
Trigger: Calling tree.insertDtr(dtr, position) with position > tree.children().length, e.g. computing the index from a stale or differently-sized daughter array, or appending with position = kids.length + 1 instead of kids.length.
Common situations: Programmatic tree rewriting in parsers/annotators that insert nodes; off-by-one errors when the daughter list changed since the position was computed; using 1-based indices in a 0-based API.
Related errors
- Error -- tree does not contain
- Tree is not a descendant of root.
- ancestor: height cannot be negative
- Attempt to open file with null name
- Bad arguments: " + x + " and " + lambda
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/b3b59defb55e8e2d.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/Tree.java:2669
*/
public List<Tree> siblings(Tree root) {
Tree parent = parent(root);
if (parent == null) {
return null;
}
List<Tree> siblings = parent.getChildrenAsList();
siblings.remove(this);
return siblings;
}
/**
* insert {@code dtr} after {@code position} existing
* daughters in {@code this}.
*/
public void insertDtr(Tree dtr, int position) {
Tree[] kids = children();
if (position > kids.length) {
throw new IllegalArgumentException("Can't insert tree after the " + position + "th daughter in " + this + "; only " + kids.length + " daughters exist!");
}
Tree[] newKids = new Tree[kids.length + 1];
int i = 0;
for (; i < position; i++) {
newKids[i] = kids[i];
}
newKids[i] = dtr;
for (; i < kids.length; i++) {
newKids[i + 1] = kids[i];
}
setChildren(newKids);
}
// --- composition methods to implement Label interface
@Override
public String value() {
Label lab = label();View on GitHub (pinned to 1b7edd19c4)