stanfordnlp/CoreNLP · error · UnsupportedOperationException

Can't set Tree labels

Error message

Can't set Tree labels

What it means

Tree.setLabels(Collection<Label>) is deliberately unimplemented: a Tree's identity is defined by its labeled nodes, so bulk-replacing the label collection is not a meaningful operation. Any call immediately throws UnsupportedOperationException.

Solutions

  1. Set labels per-node instead: call tree.label().setValue(...) on individual nodes
  2. Traverse the tree and update each node's label via a TreeTransformer or a manual recursion
  3. Remove the setLabels call; restructure code to operate on node labels rather than the whole tree

Example fix

// before
tree.setLabels(newLabels); // throws
// after
for (Tree node : tree) {
  node.label().setValue(nextValue());
}
Defensive patterns

Strategy: validation

Validate before calling

if (node instanceof Tree) { /* setLabels unsupported; mutate node.label() per node instead */ }

Type guard

boolean supportsSetLabels(Object o) { return !(o instanceof Tree) && o instanceof Labeled; }

Try / catch

try { tree.setLabels(labels); } catch (UnsupportedOperationException e) { /* rewrite via per-node label updates */ }

Prevention

When it happens

Trigger: Calling setLabels(...) on any Tree instance, often because code written against a generic Labeled interface treats the Tree like a simple labeled edge — e.g. copying labels between structures in a loop.

Common situations: Generic label-manipulation utilities operating on Label/Labeled abstractions; ported code from other tree libraries where setLabels exists; reflection- or framework-driven property setters invoking it.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/1061a936ee765585. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/Tree.java:1763

   *
   * @return the {@code Collection} (actually, Set) of all values
   *         in the tree.
   */
  @Override
  public Collection<Label> labels() {
    Set<Label> n = Generics.newHashSet();
    n.add(label());
    Tree[] kids = children();
    for (Tree kid : kids) {
      n.addAll(kid.labels());
    }
    return n;
  }


  @Override
  public void setLabels(Collection<Label> c) {
    throw new UnsupportedOperationException("Can't set Tree labels");
  }


  /**
   * Return a flattened version of a tree.  In many circumstances, this
   * will just return the tree, but if the tree is something like a
   * binarized version of a dependency grammar tree, then it will be
   * flattened back to a dependency grammar tree representation.  Formally,
   * a node will be removed from the tree when: it is not a terminal or
   * preterminal, and its {@code label()} is {@code equal()} to
   * the {@code label()} of its parent, and all its children will
   * then be promoted to become children of the parent (in the same
   * position in the sequence of daughters.
   *
   * @return A flattened version of this tree.
   */
  public Tree flatten() {
    return flatten(treeFactory());

View on GitHub (pinned to 1b7edd19c4)