stanfordnlp/CoreNLP · error · ClassCreationException
Argument " + i + " to class constructor is null
Error message
Argument " + i + " to class constructor is null
What it means
MetaClass.ClassFactory derives constructor parameter types by calling getClass() on each supplied argument. Because getClass() on null is impossible and a null constructor argument cannot be typed, the factory immediately throws ClassCreationException for any null argument.
Solutions
- Replace null arguments with explicit typed defaults before calling createInstance
- Use a constructor overload that accepts no such argument, or configure the field afterward via setters
- Guard the args array: check for nulls and supply class-typed defaults (e.g. empty String/0/false)
- Wrap construction in try-catch for ClassCreationException and log which argument was null
Example fix
// before factory.createInstance(name, null); // throws // after factory.createInstance(name, defaultValue != null ? defaultValue : DEFAULT_VALUE);
Defensive patterns
Strategy: validation
Validate before calling
for (int i = 0; i < args.length; i++) {
if (args[i] == null) throw new IllegalArgumentException("arg " + i + " is null");
}
T obj = MetaClass.create(classname).createInstance(args); Type guard
boolean hasNoNulls(Object[] args) {
return java.util.Arrays.stream(args).noneMatch(java.util.Objects::isNull);
} Try / catch
try {
T obj = MetaClass.create(classname).createInstance(args);
} catch (MetaClass.ClassCreationException e) {
if (e.getMessage() != null && e.getMessage().contains("is null")) {
// substitute defaults for null args and retry
} else { throw e; }
} Prevention
- Substitute defaults for optional config values before instantiation
- Assert args array is fully populated before calling createInstance
- Avoid passing null placeholders in reflection-based construction
- Validate config-derived arguments early at startup
When it happens
Trigger: Calling MetaClass.create(...).createInstance(a, null, c) — i.e. passing null among the constructor arguments, or programmatically building an args array where some entries were not initialized.
Common situations: Config-driven instantiation where an optional option resolved to null, reflection helper code passing null placeholders, test code constructing components with missing dependencies.
Related errors
- Cannot handle null mode
- No constructor found to match: " + target
- No governor given for an AddDep
- No relation given for an AddDep
- Annotation field cannot be null
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/db1539cafcaa95f9.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/MetaClass.java:198
}
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();
}
// (generic construct)
construct(classname, classParams);
}
private ClassFactory(String classname, String... params)
throws ClassNotFoundException, NoSuchMethodException {
// (convert class parameters)
Class<?>[] classParams = new Class[params.length];
for (int i = 0; i < params.length; i++) {
classParams[i] = Class.forName(params[i]);
}
// (generic construct)
construct(classname, classParams);
}
/**View on GitHub (pinned to 1b7edd19c4)