stanfordnlp/CoreNLP · error · ConstructorNotFoundException

No constructor found to match: " + target

Error message

No constructor found to match: " + target

What it means

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).

Solutions

  1. Inspect the target class's declared constructors and match argument types exactly
  2. Ensure primitive/boxing matches (use Integer.class vs int.class as the class expects)
  3. Check for a version change of the target library and update arguments or pin the version
  4. Use a no-arg constructor if available and configure via setters/properties instead

Example fix

// before
MetaClass.create("com.example.Foo").createInstance("42"); // Foo has no (String) ctor
// after
MetaClass.create("com.example.Foo").createInstance(Integer.parseInt("42"));
Defensive patterns

Strategy: try-catch

Validate before calling

boolean matches = false;
for (Constructor<?> ctor : Class.forName(classname).getDeclaredConstructors()) {
  Class<?>[] ps = ctor.getParameterTypes();
  if (ps.length == args.length) { matches = true; for (int i=0;i<ps.length;i++) {
    if (!ps[i].isAssignableFrom(args[i].getClass())) { matches = false; break; } } }
  if (matches) break;
}
if (!matches) throw new IllegalArgumentException("no matching constructor on " + classname);

Try / catch

try {
  T obj = MetaClass.create(classname).createInstance(args);
} catch (MetaClass.ConstructorNotFoundException e) {
  // signature mismatch: log requested signature from e.getMessage() and adjust args
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/3a2992c393b63188. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/MetaClass.java:182

            if (dist >= 0) { // and if the constructor matches...
              distances[conIndex] += dist; // keep it
            } else {
              potentials[conIndex] = null; // else, remove it from the pool
              distances[conIndex] = -1;
            }
          }
        }
      }
      // (filter:min)
      this.constructor = (Constructor<T>) argmin(potentials, distances, 0);
      if (this.constructor == null) {
        StringBuilder b = new StringBuilder();
        b.append(classname).append("(");
        for (Class<?> c : params) {
          b.append(c.getName()).append(", ");
        }
        String target = b.substring(0, params.length==0 ? b.length() : b.length() - 2) + ")";
        throw new ConstructorNotFoundException(
            "No constructor found to match: " + target);
      }
    }

    private ClassFactory(String classname, Class<?>... params)
        throws ClassNotFoundException, NoSuchMethodException {
      // (generic construct)
      construct(classname, params);
    }

    private ClassFactory(String classname, Object... params)
        throws ClassNotFoundException, NoSuchMethodException {
      // (convert class parameters)
      Class<?>[] classParams = new Class[params.length];
      for (int i = 0; i < params.length; i++) {
        if(params[i] == null) throw new ClassCreationException("Argument " + i + " to class constructor is null");
        classParams[i] = params[i].getClass();
      }

View on GitHub (pinned to 1b7edd19c4)