{"record":{"id":"dbf2e23aa8caafaf","repo":"stanfordnlp/CoreNLP","slug":"can-t-return-head-of-null-or-leaf-tree","errorCode":null,"errorMessage":"Can't return head of null or leaf Tree.","messagePattern":"Can't return head of null or leaf Tree\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/AbstractCollinsHeadFinder.java","lineNumber":164,"sourceCode":"\n  /**\n   * Determine which daughter of the current parse tree is the head.\n   *\n   * @param t The parse tree to examine the daughters of.\n   *          If this is a leaf, {@code null} is returned\n   * @param parent The parent of t\n   * @return The daughter parse tree that is the head of {@code t}.\n   *   Returns null for leaf nodes.\n   * @see Tree#percolateHeads(HeadFinder)\n   *      for a routine to call this and spread heads throughout a tree\n   */\n  @Override\n  public Tree determineHead(Tree t, Tree parent) {\n    if (nonTerminalInfo == null) {\n      throw new IllegalStateException(\"Classes derived from AbstractCollinsHeadFinder must create and fill HashMap nonTerminalInfo.\");\n    }\n    if (t == null || t.isLeaf()) {\n      throw new IllegalArgumentException(\"Can't return head of null or leaf Tree.\");\n    }\n    if (DEBUG) {\n      log.info(\"determineHead for \" + t.value());\n    }\n\n    Tree[] kids = t.children();\n\n    Tree theHead;\n    // first check if subclass found explicitly marked head\n    if ((theHead = findMarkedHead(t)) != null) {\n      if (DEBUG) {\n        log.info(\"Find marked head method returned \" +\n                           theHead.label() + \" as head of \" + t.label());\n      }\n      return theHead;\n    }\n\n    // if the node is a unary, then that kid must be the head","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/AbstractCollinsHeadFinder.java#L146-L182","documentation":"AbstractCollinsHeadFinder.determineHead computes the head child of a constituent using the nonTerminalInfo rule table. It refuses to operate on a null tree or a leaf (pre-terminal/word) node, because a head is only meaningful for a non-terminal with children. Throwing here signals a caller bug: head lookup was invoked on a node that can never have a head rule.","triggerScenarios":"Calling determineHead(t) or determineHead(t, parent) with t == null, or with a leaf Tree (t.isLeaf() true, e.g. a word node from a Tree whose leaves were not wrapped in pre-terminals).","commonSituations":"Tree transformation code that walks all children including leaves and calls determineHead on each; parsing a tree where leaves were attached directly instead of under POS pre-terminals; passing a null tree from a failed read or an empty parse result.","solutions":["Check that the tree is non-null and non-leaf before calling determineHead: if (t != null && !t.isLeaf()) head = hf.determineHead(t, parent);","Fix tree loading so leaves are always under pre-terminal (POS) nodes — trees from tokenized/leaf-attached sources must be re-bracketed.","Verify the caller recursion only visits non-terminal children (t.children() of internal nodes)."],"exampleFix":"// before\nTree head = headFinder.determineHead(child, tree);\n// after\nif (child != null && !child.isLeaf()) {\n  Tree head = headFinder.determineHead(child, tree);\n}","handlingStrategy":"validation","validationCode":"if (tree == null || tree.isLeaf()) { throw new IllegalArgumentException(\"determineHead requires a non-leaf tree\"); }","typeGuard":"boolean hasHead(Tree t) { return t != null && !t.isLeaf() && t.children().length > 0; }","tryCatchPattern":"try { head = hf.determineHead(t, parent); } catch (IllegalArgumentException e) { head = null; /* leaf/null node: no head applies */ }","preventionTips":["Guard with t != null && !t.isLeaf() before any determineHead call","Ensure parsed trees keep POS pre-terminals above leaves","In recursive tree walkers, only call head finding on internal nodes"],"tags":["java","nlp","parsing","null-argument"],"backgroundTag":"null-argument","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"}