{"record":{"id":"76d046e99c22e8d7","repo":"stanfordnlp/CoreNLP","slug":"cannot-set-from-string-76d046","errorCode":null,"errorMessage":"Cannot set from string","messagePattern":"Cannot set from string","errorType":"exception","errorClass":"java.lang.UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/IndexedWord.java","lineNumber":587,"sourceCode":"  public String toString() {\n    return toString(CoreLabel.OutputFormat.VALUE_TAG);\n  }\n\n  /** Allows choices of how to format Label.\n   *\n   * @param format An instance of the OutputFormat enum (the same as for a CoreLabel)\n   * @return A String representation of the label, including marking copy nodes with primes (')\n   */\n  public String toString(CoreLabel.OutputFormat format) {\n    return label.toString(format) + toPrimes();\n  }\n\n  /**\n   * {@inheritDoc}\n   */\n  @Override\n  public void setFromString(String labelStr) {\n    throw new UnsupportedOperationException(\"Cannot set from string\");\n  }\n\n\n  public static LabelFactory factory() {\n    return new LabelFactory() {\n\n      @Override\n      public Label newLabel(String labelStr) {\n        CoreLabel coreLabel = new CoreLabel();\n        coreLabel.setValue(labelStr);\n        return new IndexedWord(coreLabel);\n      }\n\n      @Override\n      public Label newLabel(String labelStr, int options) {\n        return newLabel(labelStr);\n      }\n","sourceCodeStart":569,"sourceCodeEnd":605,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/IndexedWord.java#L569-L605","documentation":"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.","triggerScenarios":"Calling setFromString(String) on an IndexedWord directly, or generic label-copying/serialization utilities that call label.setFromString(...) on labels of type IndexedWord.","commonSituations":"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.","solutions":["Construct the IndexedWord from a CoreLabel instead: new IndexedWord(coreLabel) or IndexedWord.valueOf(...).","Copy fields individually: iw.setWord/setTag/setIndex/setSentIndex as needed.","If you have a string form, parse it into a CoreLabel (setWord/setTag etc.) and then wrap it in an IndexedWord."],"exampleFix":"// before\nIndexedWord iw = new IndexedWord();\niw.setFromString(\"word-3\"); // throws UnsupportedOperationException\n// after\nCoreLabel cl = new CoreLabel();\ncl.setWord(\"word\");\nIndexedWord iw = new IndexedWord(cl);\niw.setIndex(3);","handlingStrategy":"try-catch","validationCode":"if (label instanceof IndexedWord) {\n  throw new IllegalStateException(\"IndexedWord.setFromString is unsupported; build from a CoreLabel instead\");\n}","typeGuard":"boolean canSetFromString(Label l) {\n  return !(l instanceof IndexedWord) && !(l instanceof CoreLabel);\n}","tryCatchPattern":"try {\n  iw.setFromString(str);\n} catch (UnsupportedOperationException e) {\n  CoreLabel cl = new CoreLabel();\n  cl.setWord(parseWord(str));\n  IndexedWord rebuilt = new IndexedWord(cl);\n  rebuilt.setIndex(parseIndex(str));\n  iw = rebuilt;\n}","preventionTips":["Build IndexedWords from CoreLabels extracted from annotations, never from strings.","Avoid generic Label serialization frameworks that assume setFromString exists for all labels.","Use IndexedWord.valueOf(CoreLabel) for construction.","Persist IndexedWords via their underlying CoreAnnotation keys rather than string forms."],"tags":["java","nlp","unsupported-operation","indexedword"],"backgroundTag":"unsupported-operation","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}