stanfordnlp/CoreNLP · error · RuntimeException

Cannot instantiate

Error message

Cannot instantiate ${c}

What it means

When a function name resolves to a Class, the library reflectively finds a constructor compatible with the evaluated arguments. If matching fails (constructor null after scanning, inner exception `ex`), it wraps the failure in RuntimeException("Cannot instantiate " + c).

Solutions

  1. Pass arguments that exactly match one of the class's public constructor signatures
  2. Ensure the target class is public and has a usable constructor
  3. Instantiate the object in Java code and bind it into the Env instead of constructing it from the rule
  4. Check the wrapped cause (`ex`) in the stack trace for the real failure (NoSuchMethodException vs probe exception)

Example fix

// before
$java.lang.String(1, 2, 3) // no such constructor
// after
$java.lang.String("abc")
Defensive patterns

Strategy: validation

Validate before calling

Class<?> c = Class.forName("com.example.Foo");
boolean hasCtor = java.util.Arrays.stream(c.getConstructors())
    .anyMatch(k -> k.getParameterCount() == args.length);

Try / catch

try {
    Value v = expr.evaluate(env, args);
} catch (RuntimeException e) {
    if (e.getMessage().startsWith("Cannot instantiate ")) {
        Throwable cause = e.getCause(); // real reflection failure
        // fall back to constructing the object in Java code
    } else throw e;
}

Prevention

When it happens

Trigger: Evaluating `com.example.Foo(args)` where no public constructor of Foo accepts the evaluated argument types (or the constructor threw while being probed), so reflective instantiation cannot proceed.

Common situations: Using the FROMCLASS / class-creation syntax in TokensRegex rules with wrong constructor arguments, non-public constructors, or refactored classes whose constructors changed after an upgrade.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java:999

            return new PrimitiveValue<>(function, obj);
          }
        }
        try {
          Constructor constructor = null;
          try {
            constructor = c.getConstructor(paramTypes);
          } catch (NoSuchMethodException ex) {
            Constructor[] constructors = c.getConstructors();
            for (Constructor cons:constructors) {
              Class[] consParamTypes = cons.getParameterTypes();
              boolean compatible = isArgTypesCompatible(paramTypes, consParamTypes);
              if (compatible) {
                constructor = cons;
                break;
              }
            }
            if (constructor == null) {
              throw new RuntimeException("Cannot instantiate " + c, ex);
            }
          }
          Object obj = constructor.newInstance(objs);
          return new PrimitiveValue<>(function, obj);
        } catch (InvocationTargetException | InstantiationException | IllegalAccessException ex) {
          throw new RuntimeException("Cannot instantiate " + c, ex);
        }
      } else {
        throw new UnsupportedOperationException("Unsupported function value " + funcValue);
      }
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof FunctionCallExpression)) return false;

      FunctionCallExpression that = (FunctionCallExpression) o;

View on GitHub (pinned to 1b7edd19c4)