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

  1. Replace null arguments with explicit typed defaults before calling createInstance
  2. Use a constructor overload that accepts no such argument, or configure the field afterward via setters
  3. Guard the args array: check for nulls and supply class-typed defaults (e.g. empty String/0/false)
  4. 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

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


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)