{"record":{"id":"5cc933cd9d392ea2","repo":"stanfordnlp/CoreNLP","slug":"object-getclass-cannot-be-cast-into-a-edu-stan","errorCode":null,"errorMessage":"{object.getClass()} cannot be cast into a edu.stanford.nlp.ie.KBPStatisticalExtractor","messagePattern":"(.+?) cannot be cast into a edu\\.stanford\\.nlp\\.ie\\.KBPStatisticalExtractor","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/KBPEnsembleExtractor.java","lineNumber":89,"sourceCode":"  }\n\n  public static void main(String[] args) throws IOException, ClassNotFoundException {\n    RedwoodConfiguration.standard().apply();  // Disable SLF4J crap.\n    ArgumentParser.fillOptions(KBPEnsembleExtractor.class, args);\n\n    KBPRelationExtractor statisticalExtractor;\n    if (STATISTICAL_MODEL.length() == 0) {\n        logger.info(\"No statistical model will be used.\");\n        statisticalExtractor = null;\n    } else {\n        Object object = IOUtils.readObjectFromURLOrClasspathOrFileSystem(STATISTICAL_MODEL);\n        if (object instanceof LinearClassifier) {\n          //noinspection unchecked\n          statisticalExtractor = new KBPStatisticalExtractor((Classifier<String, String>) object);\n        } else if (object instanceof KBPStatisticalExtractor) {\n          statisticalExtractor = (KBPStatisticalExtractor) object;\n        } else {\n          throw new ClassCastException(object.getClass() + \" cannot be cast into a \" + KBPStatisticalExtractor.class);\n        }\n        logger.info(\"Read statistical model from \" + STATISTICAL_MODEL);\n    }\n\n    KBPRelationExtractor extractor;\n    if (statisticalExtractor == null) {\n        extractor = new KBPEnsembleExtractor(\n            new KBPTokensregexExtractor(TOKENSREGEX_DIR),\n            new KBPSemgrexExtractor(SEMGREX_DIR));\n    } else {\n        extractor = new KBPEnsembleExtractor(\n            new KBPTokensregexExtractor(TOKENSREGEX_DIR),\n            new KBPSemgrexExtractor(SEMGREX_DIR),\n            statisticalExtractor);\n    }\n\n    List<Pair<KBPInput, String>> testExamples = KBPRelationExtractor.readDataset(TEST_FILE);\n","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/KBPEnsembleExtractor.java#L71-L107","documentation":"KBPEnsembleExtractor's main, when loading the statistical model file, requires the deserialized object to be either a LinearClassifier<String,String> or an existing KBPStatisticalExtractor. Any other object type is rejected with this ClassCastException.","triggerScenarios":"Passing -stat.serialize.model (statistical model) pointing to a file containing an object of an unexpected type (e.g. a different model class, a Map, or text file bytes deserialized as something else).","commonSituations":"Pointing the stat model option at the wrong serialized file (e.g. the relation extractor model instead of the statistical model); models produced by incompatible KBP training code versions.","solutions":["Point the statistical model option at the correct KBPStatisticalExtractor/LinearClassifier serialized file","Re-run the KBP statistical trainer to produce a proper model file","Inspect the object's actual class in the wrapped file to identify what was serialized","Verify you are using matching Stanford CoreNLP versions for training and loading"],"exampleFix":"// before\nargs.put(\"stat.serialize.model\", \"models/relation-extractor.ser.gz\"); // wrong file\n// after\nargs.put(\"stat.serialize.model\", \"models/kbp-statistical-model.ser.gz\"); // LinearClassifier file","handlingStrategy":"try-catch","validationCode":"try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(statModelPath))) {\n  Object o = in.readObject();\n  if (!(o instanceof LinearClassifier) && !(o instanceof KBPStatisticalExtractor))\n    throw new IllegalStateException(\"stat model file holds \" + o.getClass());\n}","typeGuard":"boolean isStatModel(Object o) {\n  return o instanceof LinearClassifier || o instanceof KBPStatisticalExtractor;\n}","tryCatchPattern":"try {\n  runKBPMain(args);\n} catch (ClassCastException e) {\n  log.error(\"wrong statistical model file type: \" + e.getMessage());\n  throw e;\n}","preventionTips":["Keep stat model and relation extractor model files clearly named/separated","Load model files produced by the same CoreNLP version's trainer","Sanity-check serialized object types after deployment upgrades"],"tags":["java","corenlp","kbp","classcast"],"backgroundTag":"type-mismatch","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"}