{"record":{"id":"d8e69ad9bf88ec77","repo":"stanfordnlp/CoreNLP","slug":"wanted-lexicalizedparser-got-object-getclass","errorCode":null,"errorMessage":"Wanted LexicalizedParser, got ${object.getClass()}","messagePattern":"Wanted LexicalizedParser, got (.+?)","errorType":"exception","errorClass":"java.lang.ClassCastException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/common/ParserGrammar.java","lineNumber":241,"sourceCode":"\n            ois.close();\n            in.close();\n          }\n        }\n      } else {\n        throw new FileNotFoundException(\"Could not find \" + modelName +\n                                        \" inside \" + zipFilename);\n      }\n    } catch (IOException e) {\n      throw new RuntimeIOException(e);\n    } catch (ClassNotFoundException e) {\n      throw new RuntimeException(e);\n    }\n\n    if (object instanceof ParserGrammar) {\n      return (ParserGrammar) object;\n    }\n    throw new ClassCastException(\"Wanted LexicalizedParser, got \" +\n                                 object.getClass());\n  }\n\n\n}\n","sourceCodeStart":223,"sourceCodeEnd":247,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/common/ParserGrammar.java#L223-L247","documentation":"ParserGrammar.loadModelFromZip() throws this ClassCastException-style RuntimeException when the object deserialized from the zip entry is not an instance of ParserGrammar. The message reports the actual class of the loaded object. Historically the expected type was LexicalizedParser, hence the wording.","triggerScenarios":"Passing a zip whose entry contains some other serialized object (a different model type, a Pipeline, an arbitrary serialized blob) to loadModelFromZip.","commonSituations":"Mixing up model files, e.g. trying to load a tagger model zip as a parser; loading a model produced by an incompatible library version whose serialized class changed.","solutions":["Verify the zip contains a serialized ParserGrammar/LexicalizedParser model","Use the correct loader for the artifact type (e.g. Tagger's loader for tagger models)","Regenerate the model with a compatible version of the parser"],"exampleFix":"// before\nParserGrammar pg = ParserGrammar.loadModelFromZip(\"tagger.zip\", \"model\");\n// after\nMaxentTagger tagger = new MaxentTagger(\"tagger.zip\"); // correct loader for tagger models","handlingStrategy":"try-catch","validationCode":"try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(in))) { Object o = ois.readObject(); if (!(o instanceof ParserGrammar)) throw new IllegalArgumentException(\"not a parser model: \" + o.getClass()); }","typeGuard":"boolean isParserModel(Object o) { return o instanceof ParserGrammar; }","tryCatchPattern":"try { return ParserGrammar.loadModelFromZip(zip, name); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Wanted\")) { throw new IOException(\"artifact is not a ParserGrammar; use the right loader\", e); } throw e; }","preventionTips":["Use the loader matching the artifact type (parser vs tagger vs pipeline)","Confirm model provenance and library version before loading","Serialize models only with the same (or compatible) library version that reads them"],"tags":["java","classcast","model-loading"],"backgroundTag":"incompatible-source-type","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"}