{"record":{"id":"2a96e748678ac706","repo":"stanfordnlp/CoreNLP","slug":"cannot-set-from-string","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/CoreLabel.java","lineNumber":362,"sourceCode":"  public <KEY extends Key<String>> String getString(Class<KEY> key) {\n    return this.getString(key, \"\");\n  }\n\n  @Override\n  public <KEY extends Key<String>> String getString(Class<KEY> key, String def) {\n    String value = get(key);\n    if (value == null) {\n      return def;\n    }\n    return value;\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   * {@inheritDoc}\n   */\n  @Override\n  public final void setValue(String value) {\n    set(CoreAnnotations.ValueAnnotation.class, value);\n  }\n\n  /**\n   * {@inheritDoc}\n   */\n  @Override\n  public final String value() {\n    return get(CoreAnnotations.ValueAnnotation.class);\n  }\n","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/CoreLabel.java#L344-L380","documentation":"CoreLabel.setFromString always throws UnsupportedOperationException: a CoreLabel holds a map of key/value pairs and has no defined textual representation that can be parsed back, so the API refuses to implement string-based label construction. It exists only to satisfy the AbstractMapLabel/Label interface contract. Any call to setFromString on a CoreLabel is by definition unsupported.","triggerScenarios":"Calling CoreLabel.setFromString(String) directly, or routing a generic Label through code that calls label.setFromString(...) (e.g. LabelFactory-style utilities that reconstruct labels from strings) when the label is actually a CoreLabel.","commonSituations":"Generic serialization/deserialization code that round-trips labels via toString/setFromString; pipeline code that copies labels from a String token source into CoreLabels; migrating code written for StringLabel/TaggedWord (which support setFromString) to CoreLabel.","solutions":["Do not call setFromString on CoreLabel; construct it via new CoreLabel(String) is also unsupported — instead build the CoreLabel and set fields with setWord/setTag/setCategory/setValue.","Use the label factory: new CoreLabelFactory().newLabelFromString(str) only if a string form was produced by that factory, otherwise parse the string yourself and set individual keys.","If you have an existing Label, create a CoreLabel via new CoreLabel(existingLabel) (copy constructor) rather than a string round-trip."],"exampleFix":"// before\nCoreLabel cl = new CoreLabel();\ncl.setFromString(\"word/PoS\"); // throws UnsupportedOperationException\n// after\nString[] parts = \"word/PoS\".split(\"/\");\nCoreLabel cl = new CoreLabel();\ncl.setWord(parts[0]);\ncl.setTag(parts[1]);","handlingStrategy":"try-catch","validationCode":"if (label instanceof CoreLabel) {\n  throw new IllegalStateException(\"CoreLabel does not support setFromString; set fields individually\");\n}","typeGuard":"boolean supportsSetFromString(Label l) {\n  return !(l instanceof CoreLabel) && !(l instanceof IndexedWord);\n}","tryCatchPattern":"try {\n  label.setFromString(str);\n} catch (UnsupportedOperationException e) {\n  // fall back to per-field construction: setWord/setTag/setCategory\n  CoreLabel cl = new CoreLabel();\n  cl.setWord(parseWord(str));\n  cl.setTag(parseTag(str));\n  label = cl;\n}","preventionTips":["Never route labels through generic toString/setFromString round-trips; use copy constructors (new CoreLabel(existingLabel)).","Check the Label interface javadoc: setFromString is optional; instanceof-check before calling.","Construct CoreLabels from token pipelines (CoreAnnotations) instead of parsing strings.","Add a unit test that exercises your serialization path with CoreLabel and IndexedWord."],"tags":["java","nlp","unsupported-operation","corelabel"],"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"}