{"record":{"id":"4198d8a4488c14de","repo":"stanfordnlp/CoreNLP","slug":"could-not-find-root","errorCode":null,"errorMessage":"Could not find root","messagePattern":"Could not find root","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/dvparser/ParseAndPrintMatrices.java","lineNumber":62,"sourceCode":"  }\n\n  public static void outputTreeMatrices(BufferedWriter bout, Tree tree, IdentityHashMap<Tree, SimpleMatrix> vectors) throws IOException {\n    if (tree.isPreTerminal() || tree.isLeaf()) {\n      return;\n    }\n    for (int i = tree.children().length - 1; i >= 0; i--) {\n      outputTreeMatrices(bout, tree.children()[i], vectors);\n    }\n    outputMatrix(bout, vectors.get(tree));\n  }\n\n  public static Tree findRootTree(IdentityHashMap<Tree, SimpleMatrix> vectors) {\n    for (Tree tree : vectors.keySet()) {\n      if (tree.label().value().equals(\"ROOT\")) {\n        return tree;\n      }\n    }\n    throw new RuntimeException(\"Could not find root\");\n  }\n\n\n  public static void main(String[] args) throws IOException {\n    String modelPath = null;\n    String outputPath = null;\n    String inputPath = null;\n\n    String testTreebankPath = null;\n    FileFilter testTreebankFilter = null;\n\n\n    List<String> unusedArgs = Generics.newArrayList();\n    for (int argIndex = 0; argIndex < args.length; ) {\n      if (args[argIndex].equalsIgnoreCase(\"-model\")) {\n        modelPath = args[argIndex + 1];\n        argIndex += 2;\n      } else if (args[argIndex].equalsIgnoreCase(\"-output\")) {","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/dvparser/ParseAndPrintMatrices.java#L44-L80","documentation":"ParseAndPrintMatrices.findRootTree scans the vector map for a subtree labeled ROOT and throws RuntimeException if none exists. The DV model normally attaches a ROOT vector to every parsed tree, so its absence means the vectors map is malformed or came from an unexpected source.","triggerScenarios":"Calling findRootTree(vectors) (directly or via rootTree) with an IdentityHashMap built from trees whose top node label is not exactly \"ROOT\" (e.g. \"TOP\" or a null/empty label), or an empty map.","commonSituations":"Trees preprocessed or re-labeled by another tool (some treebanks use TOP instead of ROOT); passing partial vectors from a custom reranker query; empty tree input.","solutions":["Ensure the parsed tree's root label is exactly \"ROOT\" before extracting matrices (relabel TOP to ROOT if needed)","Check that the vectors map comes from DVModelReranker.Query.getDeepTrees() rather than a hand-built map","Handle/avoid empty inputs that produce no vectors"],"exampleFix":"// before\nTree root = findRootTree(vectors); // throws if label is TOP\n// after\nfor (Tree t : vectors.keySet()) { if (t.label().value().equals(\"TOP\")) t.label().setValue(\"ROOT\"); }\nTree root = findRootTree(vectors);","handlingStrategy":"validation","validationCode":"Tree findRootTreeSafe(IdentityHashMap<Tree, SimpleMatrix> vectors) {\n  for (Tree t : vectors.keySet()) {\n    String v = t.label().value();\n    if (v.equals(\"ROOT\") || v.equals(\"TOP\")) return t;\n  }\n  throw new IllegalArgumentException(\"No ROOT/TOP node in vector map\");\n}","typeGuard":"boolean hasRoot(IdentityHashMap<Tree, SimpleMatrix> vectors) {\n  return vectors.keySet().stream().anyMatch(t -> t.label().value().equals(\"ROOT\"));\n}","tryCatchPattern":"try {\n  Tree root = findRootTree(vectors);\n} catch (RuntimeException e) {\n  System.err.println(\"Tree missing ROOT node: \" + e.getMessage());\n}","preventionTips":["Normalize tree labels (TOP -> ROOT) before running matrix extraction","Use vectors from DVModelReranker.Query.getDeepTrees() only","Check that the tree is non-empty before processing"],"tags":["java","runtime","treebank","dvparser"],"backgroundTag":"internal-invariant-violation","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"}