{"record":{"id":"b3b59defb55e8e2d","repo":"stanfordnlp/CoreNLP","slug":"t-insert-tree-after-the","errorCode":null,"errorMessage":"t insert tree after the ","messagePattern":"t insert tree after the ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":2669,"sourceCode":"   */\n  public List<Tree> siblings(Tree root) {\n    Tree parent = parent(root);\n    if (parent == null) {\n      return null;\n    }\n    List<Tree> siblings = parent.getChildrenAsList();\n    siblings.remove(this);\n    return siblings;\n  }\n\n  /**\n   * insert {@code dtr} after {@code position} existing\n   * daughters in {@code this}.\n   */\n  public void insertDtr(Tree dtr, int position) {\n    Tree[] kids = children();\n    if (position > kids.length) {\n      throw new IllegalArgumentException(\"Can't insert tree after the \" + position + \"th daughter in \" + this + \"; only \" + kids.length + \" daughters exist!\");\n    }\n    Tree[] newKids = new Tree[kids.length + 1];\n    int i = 0;\n    for (; i < position; i++) {\n      newKids[i] = kids[i];\n    }\n    newKids[i] = dtr;\n    for (; i < kids.length; i++) {\n      newKids[i + 1] = kids[i];\n    }\n    setChildren(newKids);\n  }\n\n  // --- composition methods to implement Label interface\n\n  @Override\n  public String value() {\n    Label lab = label();","sourceCodeStart":2651,"sourceCodeEnd":2687,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L2651-L2687","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ntree.insertDtr(newNode, idx);\n// after\ntree.insertDtr(newNode, Math.min(idx, tree.children().length));","handlingStrategy":"validation","validationCode":"if (position < 0 || position > tree.children().length) throw new IllegalArgumentException(\"invalid insert position\");\ntree.insertDtr(dtr, position);","typeGuard":null,"tryCatchPattern":"try { tree.insertDtr(dtr, position); } catch (IllegalArgumentException e) { /* clamp and retry */ tree.insertDtr(dtr, tree.children().length); }","preventionTips":["Always check children().length before inserting.","Remember the API allows appending at position == kids.length only.","Recompute positions after any tree mutation."],"tags":["trees","index-out-of-range","argument-validation"],"backgroundTag":"index-out-of-bounds","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"}