{"record":{"id":"8fd75e4523e0c56a","repo":"stanfordnlp/CoreNLP","slug":"tree-valueof-tree-construction-failed","errorCode":null,"errorMessage":"Tree.valueOf() tree construction failed","messagePattern":"Tree\\.valueOf\\(\\) tree construction failed","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Tree.java","lineNumber":2412,"sourceCode":"      return valueOf(str, new LabeledScoredTreeReaderFactory());\n  }\n\n  /**\n   * This gives you a tree from a String representation (as a\n   * bracketed Tree, of the kind produced by {@code toString()},\n   * {@code pennPrint()}, or as in the Penn Treebank.\n   * It's not the most efficient thing to do for heavy duty usage.\n   *\n   * @param str The tree as a bracketed list in a String.\n   * @param trf The TreeFactory used to make the new Tree\n   * @return The Tree\n   * @throws RuntimeException If the Tree format is not valid\n   */\n  public static Tree valueOf(String str, TreeReaderFactory trf) {\n    try {\n      return trf.newTreeReader(new StringReader(str)).readTree();\n    } catch (IOException ioe) {\n      throw new RuntimeException(\"Tree.valueOf() tree construction failed\", ioe);\n    }\n  }\n\n\n  /**\n   * Return the child at some daughter index.  The children are numbered\n   * starting with an index of 0.\n   *\n   * @param i The daughter index\n   * @return The tree at that daughter index\n   */\n  public Tree getChild(int i) {\n    Tree[] kids = children();\n    return kids[i];\n  }\n\n  /**\n   * Destructively removes the child at some daughter index and returns it.","sourceCodeStart":2394,"sourceCodeEnd":2430,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Tree.java#L2394-L2430","documentation":"Tree.valueOf(str, trf) parses a bracketed tree string using the given TreeReaderFactory. If the underlying reader throws IOException during parsing, it is rethrown as a RuntimeException with this message, meaning the tree string could not be read/constructed as a valid Tree.","triggerScenarios":"Passing a malformed Penn Treebank string (unbalanced brackets, missing labels, empty input, null) to Tree.valueOf; a TreeReaderFactory whose readTree fails on the format.","commonSituations":"Trees copied from papers/logs with mangled brackets or line breaks; truncated output from a previous parse step; wrong TreeReaderFactory for the input format.","solutions":["Validate/repair the tree string: balanced parentheses, non-empty, one root node (pennPrint-style format)","Print the offending string; check for truncation upstream in your pipeline","Use the correct TreeReaderFactory for your format, or the default Tree.valueOf(str) for standard PTB bracketed format","Wrap valueOf in try-catch for RuntimeException to handle dirty inputs gracefully"],"exampleFix":"// before\nTree t = Tree.valueOf(brokenLine); // IOException wrapped -> RuntimeException\n// after\nString s = brokenLine.trim();\nif (s.isEmpty() || !balanced(s)) { skip(s); }\nTree t = Tree.valueOf(s);","handlingStrategy":"validation","validationCode":"String s = str == null ? null : str.trim();\nif (s == null || s.isEmpty() || !balancedBrackets(s)) throw new IllegalArgumentException(\"bad tree string\");","typeGuard":"boolean looksLikeTree(String s) { return s != null && s.trim().startsWith(\"(\") && s.trim().endsWith(\")\") && balancedBrackets(s.trim()); }","tryCatchPattern":"try { Tree t = Tree.valueOf(str); } catch (RuntimeException e) { log.warn(\"Unparseable tree: {}\", str); }","preventionTips":["Validate bracket balance and non-empty input before valueOf","Check upstream pipeline output for truncation","Match TreeReaderFactory to your tree format","Catch RuntimeException around valueOf for dirty data"],"tags":["nlp","trees","parsing","tree-format"],"backgroundTag":"invalid-argument-format","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}