{"record":{"id":"36a828a681ae649e","repo":"stanfordnlp/CoreNLP","slug":"no-sentiment-value-for-integer","errorCode":null,"errorMessage":"No sentiment value for integer: ","messagePattern":"No sentiment value for integer: ","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/simple/SentimentClass.java","lineNumber":60,"sourceCode":"   *\n   * @param sentiment The Integer representation of a sentiment.\n   *\n   * @return The sentiment class associated with that integer.\n   */\n  public static SentimentClass fromInt(int sentiment) {\n    switch (sentiment) {\n      case 0:\n        return VERY_NEGATIVE;\n      case 1:\n        return NEGATIVE;\n      case 2:\n        return NEUTRAL;\n      case 3:\n        return POSITIVE;\n      case 4:\n        return VERY_POSITIVE;\n      default:\n        throw new NoSuchElementException(\"No sentiment value for integer: \" + sentiment);\n    }\n  }\n}\n","sourceCodeStart":42,"sourceCodeEnd":64,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/simple/SentimentClass.java#L42-L64","documentation":"SentimentClass.fromInt maps the integer sentiment scores 0-4 (produced by the sentiment annotator) to the SentimentClass enum (VERY_NEGATIVE..VERY_POSITIVE). Integers outside 0-4 hit the default branch and throw NoSuchElementException. This protects callers from malformed or unexpected sentiment scores.","triggerScenarios":"Calling SentimentClass.fromInt(x) with x < 0 or x > 4, e.g. from a custom annotator, a misconfigured sentiment model, or manually parsed sentiment values.","commonSituations":"Feeding raw RNNCoreAnnotations scores that are not the standard 5-class Stanford sentiment values; older/moved sentiment models producing different class ids; arithmetic on sentiment integers (e.g. averaging then rounding) yielding out-of-range values.","solutions":["Only pass integers in the 0-4 range produced by the Stanford sentiment annotator","Validate/range-check the integer before calling fromInt","Use the enum constants directly instead of converting raw integers"],"exampleFix":"// before\nSentimentClass c = SentimentClass.fromInt(rawScore); // throws for rawScore=7\n// after\nif (rawScore >= 0 && rawScore <= 4) {\n  SentimentClass c = SentimentClass.fromInt(rawScore);\n}","handlingStrategy":"validation","validationCode":"if (score < 0 || score > 4) {\n  throw new IllegalArgumentException(\"Sentiment score must be 0-4, got \" + score);\n}","typeGuard":"boolean isSentimentInt(Integer i) { return i != null && i >= 0 && i <= 4; }","tryCatchPattern":"try {\n  SentimentClass c = SentimentClass.fromInt(score);\n} catch (NoSuchElementException e) {\n  c = SentimentClass.NEUTRAL; // or log and skip\n}","preventionTips":["Only convert integers produced by the Stanford 5-class sentiment annotator","Range-check before mapping","Prefer using SentimentClass constants over manual integer mapping"],"tags":["java","enum","nlp","sentiment"],"backgroundTag":"invalid-enum-value","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"}