{"record":{"id":"4ff3ab4fd2912804","repo":"stanfordnlp/CoreNLP","slug":"only-works-on-corelabel","errorCode":null,"errorMessage":"Only works on CoreLabel","messagePattern":"Only works on CoreLabel","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Trees.java","lineNumber":897,"sourceCode":"      cl.setValue(l.value());\n      tree.setLabel(cl);\n    }\n\n    for (Tree kid : tree.children()) {\n      convertToCoreLabels(kid);\n    }\n  }\n\n\n  /**\n   * Set the sentence index of all the leaves in the tree\n   * (only works on CoreLabel)\n   */\n  public static void setSentIndex(Tree tree, int sentIndex) {\n    List<Label> leaves = tree.yield();\n    for (Label leaf : leaves) {\n      if (!(leaf instanceof CoreLabel)) {\n        throw new IllegalArgumentException(\"Only works on CoreLabel\");\n      }\n      ((CoreLabel) leaf).setSentIndex(sentIndex);\n    }\n  }\n}\n","sourceCodeStart":879,"sourceCodeEnd":903,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Trees.java#L879-L903","documentation":"Trees.setSentIndex only operates on trees whose leaves are CoreLabel instances, because the sentence index annotation lives on CoreLabel. If any leaf implements Label but not CoreLabel, it throws IllegalArgumentException immediately.","triggerScenarios":"Calling Trees.setSentIndex(tree, i) on a tree built with a factory that produces non-CoreLabel leaves, e.g. LabeledScoredTreeFactory with StringLabel or Word leaves.","commonSituations":"Mixing tree factories: reading a tree with default (StringLabel-based) factories then attempting CoreNLP annotations; converting between treebanks that use different Label types.","solutions":["Build or re-read the tree with a CoreLabel-producing factory such as LabeledScoredTreeFactory(CoreLabel.factory())","Convert existing leaves to CoreLabel by rebuilding the tree with TreeCoreAnnotations or a transform that replaces labels","Set sentIndex directly by iterating leaves and casting only after instanceof CoreLabel check"],"exampleFix":"// before\nTree t = Trees.readTree(ptbStr); // leaves may be StringLabel\nTrees.setSentIndex(t, 5); // throws\n// after\nTree t = Trees.readTree(ptbStr, new LabeledScoredTreeFactory(CoreLabel.factory()));\nTrees.setSentIndex(t, 5);","handlingStrategy":"type-guard","validationCode":"for (Label leaf : tree.yield()) {\n  if (!(leaf instanceof CoreLabel)) {\n    throw new IllegalArgumentException(\"leaf \" + leaf + \" is not a CoreLabel\");\n  }\n}","typeGuard":"boolean hasCoreLeaves(Tree t) {\n  return t.yield().stream().allMatch(l -> l instanceof CoreLabel);\n}","tryCatchPattern":"try {\n  Trees.setSentIndex(tree, sentIndex);\n} catch (IllegalArgumentException e) {\n  tree = rebuildWithCoreLabels(tree);\n  Trees.setSentIndex(tree, sentIndex);\n}","preventionTips":["Build trees with LabeledScoredTreeFactory(CoreLabel.factory()) when CoreNLP annotations will be applied","Check leaf label types before annotation utilities","Standardize on CoreLabel across the pipeline"],"tags":["java","labels","corelabel"],"backgroundTag":"type-mismatch","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"}