{"record":{"id":"810ee36fb44f0ea0","repo":"stanfordnlp/CoreNLP","slug":"class-classname-could-not-be-cast-to-the-c","errorCode":null,"errorMessage":"Class \" + classname + \" could not be cast to the correct type","messagePattern":"Class \" \\+ classname \\+ \" could not be cast to the correct type","errorType":"exception","errorClass":"ClassCreationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/MetaClass.java","lineNumber":137,"sourceCode":"          }\n        }\n        if (minDist != Integer.MAX_VALUE)\n          return minDist + 1; // case: interface distance\n        else\n          return -1; // case: failure\n      }\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    private void construct(String classname, Class<?>... params)\n        throws ClassNotFoundException, NoSuchMethodException {\n      // (save class parameters)\n      this.classParams = params;\n      // (create class)\n      try {\n        this.cl = (Class<T>) Class.forName(classname);\n      } catch (ClassCastException e) {\n        throw new ClassCreationException(\"Class \" + classname\n            + \" could not be cast to the correct type\");\n      }\n      // --Find Constructor\n      // (get constructors)\n      Constructor<?>[] constructors = cl.getDeclaredConstructors();\n      Constructor<?>[] potentials = new Constructor<?>[constructors.length];\n      Class<?>[][] constructorParams = new Class<?>[constructors.length][];\n      int[] distances = new int[constructors.length]; //distance from base class\n      // (filter: length)\n      for (int i = 0; i < constructors.length; i++) {\n        constructorParams[i] = constructors[i].getParameterTypes();\n        if (params.length == constructorParams[i].length) { // length is good\n          potentials[i] = constructors[i];\n          distances[i] = 0;\n        } else { // length is bad\n          potentials[i] = null;\n          distances[i] = -1;\n        }","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/MetaClass.java#L119-L155","documentation":"MetaClass.ClassFactory loads a class by name via Class.forName and casts the resulting Class<?> to Class<T>. If the loaded class cannot be cast to the expected type parameter (e.g. the requested classname is not assignable to the generic target type), a ClassCreationException wraps the failure with the offending classname in the message.","triggerScenarios":"Configuring a classname that does not extend/implement the expected type, e.g. pointing a pipeline component option at a class of the wrong interface; the ClassCastException occurs at the unchecked cast inside the ClassFactory constructor.","commonSituations":"Typo'd or wrong FQCN in properties/serialized options, upgrading a library where a class moved or changed its interface, copy-pasting a class name from a different component type.","solutions":["Verify the configured classname implements/extends the expected component interface","Print Class.forName(classname).getInterfaces()/superclass to inspect the loaded type","Fix the classname in the configuration file or code that passes it","Rebuild against the correct library version if the class hierarchy changed"],"exampleFix":"// before\nprops.setProperty(\"classifier\", \"edu.stanford.nlp.tagger.MaxentTagger\"); // not a Classifier\n// after\nprops.setProperty(\"classifier\", \"edu.stanford.nlp.classify.ColumnDataClassifier\");","handlingStrategy":"try-catch","validationCode":"Class<?> c;\ntry { c = Class.forName(classname); }\ncatch (ClassNotFoundException e) { throw new IllegalArgumentException(\"unknown class: \" + classname); }\nif (!ExpectedType.class.isAssignableFrom(c)) {\n  throw new IllegalArgumentException(classname + \" does not implement \" + ExpectedType.class.getName());\n}","typeGuard":"boolean isAssignableTo(String classname, Class<?> target) {\n  try { return target.isAssignableFrom(Class.forName(classname)); }\n  catch (ClassNotFoundException e) { return false; }\n}","tryCatchPattern":"try {\n  T obj = MetaClass.create(classname).createInstance(args);\n} catch (MetaClass.ClassCreationException e) {\n  // wrong type: log classname and expected type, fix config\n}","preventionTips":["Validate configured classnames against the expected interface at startup","Keep config files and library versions in sync","Use constants/enums for allowed classnames instead of free-form strings","Check instanceof after loading when type is dynamic"],"tags":["reflection","classloading","stanford-corenlp"],"backgroundTag":"class-not-found","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"}