{"record":{"id":"3a2992c393b63188","repo":"stanfordnlp/CoreNLP","slug":"no-constructor-found-to-match-target","errorCode":null,"errorMessage":"No constructor found to match: \" + target","messagePattern":"No constructor found to match: \" \\+ target","errorType":"exception","errorClass":"ConstructorNotFoundException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/MetaClass.java","lineNumber":182,"sourceCode":"            if (dist >= 0) { // and if the constructor matches...\n              distances[conIndex] += dist; // keep it\n            } else {\n              potentials[conIndex] = null; // else, remove it from the pool\n              distances[conIndex] = -1;\n            }\n          }\n        }\n      }\n      // (filter:min)\n      this.constructor = (Constructor<T>) argmin(potentials, distances, 0);\n      if (this.constructor == null) {\n        StringBuilder b = new StringBuilder();\n        b.append(classname).append(\"(\");\n        for (Class<?> c : params) {\n          b.append(c.getName()).append(\", \");\n        }\n        String target = b.substring(0, params.length==0 ? b.length() : b.length() - 2) + \")\";\n        throw new ConstructorNotFoundException(\n            \"No constructor found to match: \" + target);\n      }\n    }\n\n    private ClassFactory(String classname, Class<?>... params)\n        throws ClassNotFoundException, NoSuchMethodException {\n      // (generic construct)\n      construct(classname, params);\n    }\n\n    private ClassFactory(String classname, Object... params)\n        throws ClassNotFoundException, NoSuchMethodException {\n      // (convert class parameters)\n      Class<?>[] classParams = new Class[params.length];\n      for (int i = 0; i < params.length; i++) {\n        if(params[i] == null) throw new ClassCreationException(\"Argument \" + i + \" to class constructor is null\");\n        classParams[i] = params[i].getClass();\n      }","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/MetaClass.java#L164-L200","documentation":"MetaClass.ClassFactory searches the target class's declared constructors for one matching the supplied parameter types. If none matches (after considering boxing/assignability rules implemented in the factory), it throws ConstructorNotFoundException with a rendered signature like com.example.Foo(java.lang.String, int).","triggerScenarios":"Calling MetaClass.create(...).createInstance(args) with argument types that match no declared constructor — wrong number of args, incompatible types (e.g. int vs long, String vs Integer where unsupported), or passing Class parameters that don't correspond to any constructor.","commonSituations":"Version drift where the target class changed its constructor signature, passing boxed vs primitive mismatches the factory can't reconcile, config-driven instantiation with wrong option types.","solutions":["Inspect the target class's declared constructors and match argument types exactly","Ensure primitive/boxing matches (use Integer.class vs int.class as the class expects)","Check for a version change of the target library and update arguments or pin the version","Use a no-arg constructor if available and configure via setters/properties instead"],"exampleFix":"// before\nMetaClass.create(\"com.example.Foo\").createInstance(\"42\"); // Foo has no (String) ctor\n// after\nMetaClass.create(\"com.example.Foo\").createInstance(Integer.parseInt(\"42\"));","handlingStrategy":"try-catch","validationCode":"boolean matches = false;\nfor (Constructor<?> ctor : Class.forName(classname).getDeclaredConstructors()) {\n  Class<?>[] ps = ctor.getParameterTypes();\n  if (ps.length == args.length) { matches = true; for (int i=0;i<ps.length;i++) {\n    if (!ps[i].isAssignableFrom(args[i].getClass())) { matches = false; break; } } }\n  if (matches) break;\n}\nif (!matches) throw new IllegalArgumentException(\"no matching constructor on \" + classname);","typeGuard":null,"tryCatchPattern":"try {\n  T obj = MetaClass.create(classname).createInstance(args);\n} catch (MetaClass.ConstructorNotFoundException e) {\n  // signature mismatch: log requested signature from e.getMessage() and adjust args\n}","preventionTips":["Match constructor argument count and types exactly (mind primitive vs boxed)","Pin library versions so constructor signatures stay stable","Prefer no-arg construction plus setters where available","Read the rendered signature in the message to compare with your args"],"tags":["reflection","constructor","stanford-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-17T15:17:12.973Z"}