stanfordnlp/CoreNLP · error · java.lang.UnsupportedOperationException

Cannot set from string

Error message

Cannot set from string

What it means

IndexedWord.setFromString unconditionally throws UnsupportedOperationException. IndexedWord wraps a CoreLabel plus sentence/index metadata, and there is no defined inverse mapping from an arbitrary string back to an indexed word, so the Label interface method is intentionally left unimplemented. Any code path that tries to set an IndexedWord's contents from a string will always fail.

Solutions

  1. Construct the IndexedWord from a CoreLabel instead: new IndexedWord(coreLabel) or IndexedWord.valueOf(...).
  2. Copy fields individually: iw.setWord/setTag/setIndex/setSentIndex as needed.
  3. If you have a string form, parse it into a CoreLabel (setWord/setTag etc.) and then wrap it in an IndexedWord.

Example fix

// before
IndexedWord iw = new IndexedWord();
iw.setFromString("word-3"); // throws UnsupportedOperationException
// after
CoreLabel cl = new CoreLabel();
cl.setWord("word");
IndexedWord iw = new IndexedWord(cl);
iw.setIndex(3);
Defensive patterns

Strategy: try-catch

Validate before calling

if (label instanceof IndexedWord) {
  throw new IllegalStateException("IndexedWord.setFromString is unsupported; build from a CoreLabel instead");
}

Type guard

boolean canSetFromString(Label l) {
  return !(l instanceof IndexedWord) && !(l instanceof CoreLabel);
}

Try / catch

try {
  iw.setFromString(str);
} catch (UnsupportedOperationException e) {
  CoreLabel cl = new CoreLabel();
  cl.setWord(parseWord(str));
  IndexedWord rebuilt = new IndexedWord(cl);
  rebuilt.setIndex(parseIndex(str));
  iw = rebuilt;
}

Prevention

When it happens

Trigger: Calling setFromString(String) on an IndexedWord directly, or generic label-copying/serialization utilities that call label.setFromString(...) on labels of type IndexedWord.

Common situations: Round-tripping semantic graph nodes (SemanticGraphCoreAnnotations) through string serialization; legacy code written for Word/StringLabel ported to IndexedWord; LabelFactory code paths that assume all labels support string construction.

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/76d046e99c22e8d7. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/IndexedWord.java:587

  public String toString() {
    return toString(CoreLabel.OutputFormat.VALUE_TAG);
  }

  /** Allows choices of how to format Label.
   *
   * @param format An instance of the OutputFormat enum (the same as for a CoreLabel)
   * @return A String representation of the label, including marking copy nodes with primes (')
   */
  public String toString(CoreLabel.OutputFormat format) {
    return label.toString(format) + toPrimes();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void setFromString(String labelStr) {
    throw new UnsupportedOperationException("Cannot set from string");
  }


  public static LabelFactory factory() {
    return new LabelFactory() {

      @Override
      public Label newLabel(String labelStr) {
        CoreLabel coreLabel = new CoreLabel();
        coreLabel.setValue(labelStr);
        return new IndexedWord(coreLabel);
      }

      @Override
      public Label newLabel(String labelStr, int options) {
        return newLabel(labelStr);
      }

View on GitHub (pinned to 1b7edd19c4)