stanfordnlp/CoreNLP · error · UnsupportedOperationException

Treebank is read-only

Error message

Treebank is read-only

What it means

Deliberate UnsupportedOperationException from Treebank.remove(Object): a Treebank is treated as a read-only collection view over parsed tree files, and mutation of the underlying corpus is not supported, so the single-element removal operation (and the other mutators it represents) fail fast rather than silently doing nothing or mutating state. It fires whenever client code attempts to remove a tree from a Treebank as if it were a mutable Collection.

Solutions

  1. Build a new filtered collection instead of mutating: iterate and add survivors to a List or new MemoryTreebank.
  2. If file-backed removal is needed, rewrite the corpus file without the unwanted trees.
  3. Wrap the Treebank in a custom Collection implementing remove if mutation is truly required.

Example fix

// before
treebank.remove(tree);
// after
List<Tree> kept = new ArrayList<>();
for (Tree t : treebank) if (keep(t)) kept.add(t);
Defensive patterns

Strategy: try-catch

Try / catch

try { treebank.remove(tree); } catch (UnsupportedOperationException e) { /* filter into a new collection instead */ }

Prevention

When it happens

Trigger: Calling treebank.remove(treeOrObject), or any bulk operation that relies on removal such as treebank.removeAll(...), retainAll(...), or Iterator.remove() backed by this collection.

Common situations: Trying to filter a loaded corpus by deleting trees; generic Collection-processing code that mutates its argument; removing items while iterating a Treebank.

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/2a180cdf39c6db54. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/Treebank.java:507

        }
      }
      pw.println("    Cats: " + Counters.toString(cats, nf));
      pw.println("    Tags: " + Counters.toString(tags, nf));
      pw.println("    " + starts.size() + " start categories: " + Counters.toString(starts, nf));
      if ( ! puncts.isEmpty()) {
        pw.println("    Puncts: " + Counters.toString(puncts, nf));
      }
    }
    return sw.toString();
  }


  /**
   * This operation isn't supported for a Treebank.  Tell them immediately.
   */
  @Override
  public boolean remove(Object o) {
    throw new UnsupportedOperationException("Treebank is read-only");
  }

}

View on GitHub (pinned to 1b7edd19c4)