{"record":{"id":"509fb6950db00bab","repo":"stanfordnlp/CoreNLP","slug":"constituent-can-t-be-multilabeled","errorCode":null,"errorMessage":"Constituent can't be multilabeled","messagePattern":"Constituent can't be multilabeled","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/Constituent.java","lineNumber":78,"sourceCode":"  /**\n   * Sets the label associated with the current Constituent,\n   * if there is one.\n   */\n  public void setLabel(Label label) {\n    // a noop\n  }\n\n\n  /**\n   * Access labels -- actually always a singleton here.\n   */\n  public Collection<Label> labels() {\n    return Collections.singletonList(label());\n  }\n\n\n  public void setLabels(Collection<Label> labels) {\n    throw new UnsupportedOperationException(\"Constituent can't be multilabeled\");\n  }\n\n\n  /**\n   * access score\n   */\n  public double score() {\n    return Double.NaN;\n  }\n\n\n  /**\n   * Sets the score associated with the current node, if there is one\n   */\n  public void setScore(double score) {\n    // a no-op\n  }\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/Constituent.java#L60-L96","documentation":"The base Constituent class supports only a single label (label()/setLabel()); its setLabels(Collection) contract from Labeled exists for subclasses that allow multiple labels. The default implementation throws UnsupportedOperationException because a plain Constituent cannot be multilabeled.","triggerScenarios":"Calling setLabels(...) on any Constituent instance that is not a multilabeled subclass (e.g. on a LabeledConstituent used via the base Constituent API or on a simple Constituent).","commonSituations":"Generic code that re-labels Labeled objects (e.g. tree normalization or filter pipelines) hitting a Constituent; copying labels between constituents assuming Labeled semantics.","solutions":["Use setLabel(Label) (singular) instead of setLabels(Collection) on plain constituents.","Use a multilabeled constituent subclass that overrides setLabels if multiple labels are truly needed.","Check the runtime type before calling setLabels and route accordingly."],"exampleFix":"// before\nconstituent.setLabels(Collections.singletonList(label));\n// after\nconstituent.setLabel(label);","handlingStrategy":"type-guard","validationCode":"if (c.labels().size() > 1 || needMultipleLabels) { useMultilabeledConstituent(c); }","typeGuard":"boolean supportsMultiLabel(Labeled l) { try { l.setLabels(l.labels()); return true; } catch (UnsupportedOperationException e) { return false; } }","tryCatchPattern":"try { c.setLabels(newLabels); } catch (UnsupportedOperationException e) { c.setLabel(newLabels.iterator().next()); }","preventionTips":["Use setLabel (singular) on base Constituents","Only call setLabels on subclasses documented as multilabeled"],"tags":["java","nlp","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}