stanfordnlp/CoreNLP · error · RuntimeException
Cannot evaluate method
Error message
Cannot evaluate method ${function} on object ${mainObj} What it means
The method was found, but invoking it reflectively failed with InvocationTargetException (the method itself threw) or IllegalAccessException (inaccessible). The library wraps it in RuntimeException("Cannot evaluate method ... on object ...").
Solutions
- Unwrap getCause() on the RuntimeException to see the method's own exception and fix the object state or arguments
- For MatchResult.group(int), only call it after confirming match succeeded (group index within match bounds)
- Make the method public, or precompute the value in Java and bind it instead
- Validate the object's state before evaluating the method call in the rule
Example fix
// before $match.group(3) // match only had 2 groups -> IndexOutOfBoundsException inside // after $match.group(1)
Defensive patterns
Strategy: try-catch
Validate before calling
// for MatchResult.group(int): guard index and match success first
if (match.groupCount() <= idx) throw new IllegalArgumentException("No group " + idx); Try / catch
try {
Value v = expr.evaluate(env, args);
} catch (RuntimeException e) {
if (e.getMessage().startsWith("Cannot evaluate method ") && e.getCause() instanceof InvocationTargetException) {
Throwable real = e.getCause().getCause(); // the method's own exception
} else throw e;
} Prevention
- Always check e.getCause() — the real failure is inside the invoked method
- Validate object state before method-call expressions (e.g. match succeeded before group())
- Use public methods only, to avoid IllegalAccessException paths
When it happens
Trigger: Evaluating `$obj.method(args)` where the invoked method threw an exception internally (InvocationTargetException — the common case), or the method is not accessible from the reflection caller (IllegalAccessException).
Common situations: Calling a getter/method on a bound object whose internal state is invalid (e.g. calling group() on a failed match), invoking methods on objects built from different library versions, or calling non-public methods.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Cannot instantiate
- Cannot find method on object of class
- Unknown field for type , trying to set to
- Incompatible type for type , trying to set to
- Unknown class
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/38ab9cfc5f1c042d.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java:1131
if (method == null) {
throw new RuntimeException("Cannot find method " + function + " on object of class " + c, ex);
}
}
try {
Object res;
if (mainObj instanceof MatchResult && method.getName().equals("group")
&& objs.length == 1 && objs[0] instanceof Integer) {
// handle case of calling MatchResult's group(int group) method
// this requires casting the mainObj to a MatchResult post Java 8 because
// Matcher's toMatchResult() now returns a Matcher$ImmutableMatchResult
res = ((MatchResult) mainObj).group((Integer) objs[0]);
} else {
// handle all other cases
res = method.invoke(mainObj, objs);
}
return new PrimitiveValue<>(function, res);
} catch (InvocationTargetException | IllegalAccessException ex) {
throw new RuntimeException("Cannot evaluate method " + function + " on object " + mainObj, ex);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MethodCallExpression)) return false;
if (!super.equals(o)) return false;
MethodCallExpression that = (MethodCallExpression) o;
if (function != null ? !function.equals(that.function) : that.function != null) return false;
if (object != null ? !object.equals(that.object) : that.object != null) return false;
if (params != null ? !params.equals(that.params) : that.params != null) return false;
return true;
}
View on GitHub (pinned to 1b7edd19c4)