{"record":{"id":"d0191c0f609bb6e0","repo":"stanfordnlp/CoreNLP","slug":"metaclass-couldn-t-create-constructor-with","errorCode":null,"errorMessage":"MetaClass couldn't create \" + constructor + \" with args \" + Arrays.toString(params)","messagePattern":"MetaClass couldn't create \" \\+ constructor \\+ \" with args \" \\+ Arrays\\.toString\\(params\\)","errorType":"exception","errorClass":"ClassCreationException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/MetaClass.java","lineNumber":237,"sourceCode":"     * @param params\n     *            The arguments to the constructor of the class NOTE: the\n     *            resulting instance will [unlike java] invoke the most\n     *            narrow constructor rather than the one which matches the\n     *            signature passed to this function\n     * @return An instance of the class\n     */\n    public T createInstance(Object... params) {\n      try {\n        boolean accessible = true;\n        if(!constructor.isAccessible()){\n          accessible = false;\n          constructor.setAccessible(true);\n        }\n        T rtn = constructor.newInstance(params);\n        if(!accessible){ constructor.setAccessible(false); }\n        return rtn;\n      } catch (Exception e) {\n        throw new ClassCreationException(\"MetaClass couldn't create \" + constructor + \" with args \" + Arrays.toString(params), e);\n      }\n    }\n\n    /**\n     * Returns the full class name for the objects being produced\n     *\n     * @return The class name for the objects produced\n     */\n    public String getName() {\n      return cl.getName();\n    }\n\n    @Override\n    public String toString() {\n      StringBuilder b = new StringBuilder();\n      b.append(cl.getName()).append('(');\n      for (Class<?> cl : classParams) {\n        b.append(' ').append(cl.getName()).append(',');","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/MetaClass.java#L219-L255","documentation":"MetaClass's reflective constructor-invocation helper wraps any exception raised by Constructor.newInstance (e.g. the target constructor threw, or was inaccessible/abstract) into a ClassCreationException. The message includes the constructor and the exact argument values used, so you can see what instantiation attempt failed. This is the Stanford CoreNLP reflection-based factory failing to build an object, typically from a properties-specified class.","triggerScenarios":"Calling MetaClass.createInstance(Class,Object...) or createInstance(Object...) where the resolved constructor throws an exception during execution, the constructor is not accessible and setAccessible fails, or the class is abstract/interface with no concrete constructor.","commonSituations":"Specifying a custom annotator/serializer class name in a CoreNLP properties file whose constructor throws; passing wrong constructor argument types so reflection resolves a constructor that then fails; refactoring a class after serializing a config that referenced it.","solutions":["Inspect the wrapped cause 'e' printed with this exception — it contains the real failure from the target constructor","Verify the constructor arguments' types exactly match a declared constructor (reflection requires exact types, no autoboxing/coercion)","Confirm the class is public, concrete (not abstract/interface), and has a matching constructor","Test the class directly with 'new MyClass(args)' outside MetaClass to reproduce the underlying error"],"exampleFix":"// before\nProperties props = new Properties();\nprops.setProperty(\"annotators\", \"myAnnotator\"); // class whose ctor throws\n// after: fix the target class constructor to not throw with given args, or use the no-arg ctor\nprops.setProperty(\"customAnnotator.myAnnotator.constructorArgs\", \"\"); // plus fix ctor","handlingStrategy":"try-catch","validationCode":"// before createInstance\nClass<?> cls = Class.forName(\"com.example.MyTypeImpl\");\nif (java.lang.reflect.Modifier.isAbstract(cls.getModifiers())) throw new IllegalStateException(\"abstract class\");\nfor (Constructor<?> c : cls.getConstructors()) {\n  if (Arrays.equals(c.getParameterTypes(), new Class[]{String.class, int.class})) { /* exact match found */ }\n}","typeGuard":null,"tryCatchPattern":"try {\n  T obj = metaClass.createInstance(Target.class, params);\n} catch (MetaClass.ClassCreationException e) {\n  logger.log(Level.SEVERE, \"Instantiation failed for args \" + Arrays.toString(params), e.getCause());\n  throw new IllegalStateException(\"Bad class/args in config\", e.getCause());\n}","preventionTips":["Always log/print the cause chain — the real error is inside the wrapped exception","Keep target classes public with public no-arg or exact-signature constructors","Test any class referenced from properties files with a direct 'new' in unit tests","Avoid renaming/refactoring classes that appear in persisted configuration"],"tags":["reflection","instantiation","java","corenlp"],"backgroundTag":"invalid-constructor-argument","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"}