{"record":{"id":"514d3d452fb3dae3","repo":"stanfordnlp/CoreNLP","slug":"wanted-lexicalizedparser-got","errorCode":null,"errorMessage":"Wanted LexicalizedParser, got ","messagePattern":"Wanted LexicalizedParser, got ","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java","lineNumber":202,"sourceCode":"    LexicalizedParser parser = getParserFromFile(parserFileOrUrl, op);\n    if (extraFlags.length > 0) {\n      parser.setOptionFlags(extraFlags);\n    }\n    return parser;\n  }\n\n  /**\n   * Reads one object from the given ObjectInputStream, which is\n   * assumed to be a LexicalizedParser.  Throws a ClassCastException\n   * if this is not true.  The stream is not closed.\n   */\n  public static LexicalizedParser loadModel(ObjectInputStream ois) {\n    try {\n      Object o = ois.readObject();\n      if (o instanceof LexicalizedParser) {\n        return (LexicalizedParser) o;\n      }\n      throw new ClassCastException(\"Wanted LexicalizedParser, got \" +\n                                   o.getClass());\n    } catch (IOException e) {\n      throw new RuntimeIOException(e);\n    } catch (ClassNotFoundException e) {\n      throw new RuntimeException(e);\n    }\n  }\n\n  public static LexicalizedParser copyLexicalizedParser(LexicalizedParser parser) {\n    return new LexicalizedParser(parser.lex, parser.bg, parser.ug, parser.dg, parser.stateIndex, parser.wordIndex, parser.tagIndex, parser.op);\n  }\n\n  public LexicalizedParser(Lexicon lex, BinaryGrammar bg, UnaryGrammar ug, DependencyGrammar dg, Index<String> stateIndex, Index<String> wordIndex, Index<String> tagIndex, Options op) {\n    this.lex = lex;\n    this.bg = bg;\n    this.ug = ug;\n    this.dg = dg;\n    this.stateIndex = stateIndex;","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java#L184-L220","documentation":"loadModel(ObjectInputStream) reads one object and expects it to be a LexicalizedParser; anything else is rejected with this ClassCastException naming the actual class received. It also converts IOException/ClassNotFoundException from stream deserialization into RuntimeIOException/RuntimeException.","triggerScenarios":"Calling LexicalizedParser.loadModel(ObjectInputStream) on a stream whose next object is not a parser — e.g. the file holds a LexicalizedParserQuery, an Options object, or unrelated serialized data.","commonSituations":"Deserializing a file saved with parserQuery/other components, or reading a file produced by an incompatible library version where class layout differs.","solutions":["Point loadModel at a file that contains a serialized LexicalizedParser (created via saveParserToSerializedFile).","Check the actual class named in the message and use the matching load API (e.g. loadParserQuery).","Use LexicalizedParser.loadModel(String) so the correct loading path is chosen for the file."],"exampleFix":"// before\nObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"query.ser\"));\nLexicalizedParser lp = LexicalizedParser.loadModel(ois); // file is a query\n// after\nLexicalizedParser lp = LexicalizedParser.loadModel(\"parser.ser.gz\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"if (o instanceof LexicalizedParser) { LexicalizedParser lp = (LexicalizedParser) o; }","tryCatchPattern":"try { parser = LexicalizedParser.loadModel(path); } catch (ClassCastException | RuntimeIOException | RuntimeException e) { log.error(\"Not a serialized parser model: \" + e.getMessage()); }","preventionTips":["Save models only with saveParserToSerializedFile and load them with loadModel(String)","Keep library versions consistent between save and load"],"tags":["deserialization","class-cast","parser","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}