{"record":{"id":"130f421419f7452b","repo":"stanfordnlp/CoreNLP","slug":"bad-tree-state","errorCode":null,"errorMessage":"Bad tree state: ","messagePattern":"Bad tree state: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Trees.java","lineNumber":493,"sourceCode":"        }\n        if (tagLabels == null) {\n          tagLabels = StringUtils.getShortClassName(slCl);\n        } else if ( ! tagLabels.equals(slCl)) {\n          tagLabels = \"mixed\";\n        }\n      } else if (st.isLeaf()) {\n        if (leaves == null) {\n          leaves = stCl;\n        } else if ( ! leaves.equals(stCl)) {\n          leaves = \"mixed\";\n        }\n        if (leafLabels == null) {\n          leafLabels = slCl;\n        } else if ( ! leafLabels.equals(slCl)) {\n          leafLabels = \"mixed\";\n        }\n      } else {\n        throw new IllegalStateException(\"Bad tree state: \" + t);\n      }\n    } // end for Tree st : this\n    StringBuilder sb = new StringBuilder();\n    sb.append(\"Tree with root of class \").append(tCl).append(\" and factory \").append(tfCl);\n    sb.append(\" and root label class \").append(lCl).append(\" and factory \").append(lfCl);\n    if ( ! otherClasses.isEmpty()) {\n      sb.append(\" and the following classes also found within the tree: \").append(otherClasses);\n      return \" with \" + nodes + \" interior nodes and \" + leaves +\n        \" leaves, and \" + phraseLabels + \" phrase labels, \" +\n        tagLabels + \" tag labels, and \" + leafLabels + \" leaf labels.\";\n    } else {\n      sb.append(\" (and uniform use of these Tree and Label classes throughout the tree).\");\n    }\n    return sb.toString();\n  }\n\n\n  /** Turns a sentence into a flat phrasal tree.","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Trees.java#L475-L511","documentation":"Thrown by Trees.toStructureDebugString when a subtree's state does not fit the expected cases the debug walker understands (e.g. a subtree that is neither a leaf with a simple label nor a node whose children classes are consistent). It indicates an internal tree-state assumption was violated while generating a structural description.","triggerScenarios":"Calling toStructureDebugString on a Tree whose internal structure violates the method's assumptions, such as a non-leaf node whose children list is null or of unexpected class, or a tree built by a custom TreeFactory producing inconsistent node types.","commonSituations":"Debugging trees constructed by third-party or hand-rolled TreeFactory implementations; trees mutated concurrently or corrupted by custom transformations before inspection.","solutions":["Inspect the offending subtree t to see why it doesn't match expected leaf/internal shapes","Build trees with standard factories (LabeledScoredTreeFactory) before calling debug utilities","Validate the tree (e.g. check children non-null for internal nodes) before invoking toStructureDebugString"],"exampleFix":"// before\nString dbg = Trees.toStructureDebugString(customTree);\n// after\nfor (Tree st : customTree) {\n  if (!st.isLeaf() && (st.children() == null || st.children().length == 0)) {\n    throw new IllegalStateException(\"malformed internal node: \" + st);\n  }\n}\nString dbg = Trees.toStructureDebugString(customTree);","handlingStrategy":"validation","validationCode":"boolean wellFormed(Tree t) {\n  for (Tree st : t) {\n    if (!st.isLeaf() && (st.children() == null || st.children().length == 0)) return false;\n  }\n  return true;\n}","typeGuard":"boolean isDebuggable(Tree t) {\n  return t != null && t.label() != null && wellFormed(t);\n}","tryCatchPattern":"try {\n  String dbg = Trees.toStructureDebugString(t);\n} catch (IllegalStateException e) {\n  logger.error(\"bad tree state for {}: {}\", t, e.getMessage());\n}","preventionTips":["Use standard tree factories when building trees","Never hand-mutate children arrays of internal nodes","Validate tree invariants before debug/inspection utilities"],"tags":["java","trees","internal-state"],"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"}