stanfordnlp/CoreNLP · error · RuntimeException

Cannot find method on object of class

Error message

Cannot find method ${function} on object of class ${c}

What it means

When the callee is an object (method-call syntax `obj.method(args)`), the library reflectively searches the object's class for a method matching the name and evaluated argument types. If no method matches (captured in `ex`, typically NoSuchMethodException or an argument-type mismatch), it throws RuntimeException("Cannot find method ...").

Solutions

  1. Verify the method name and signature exist on the object's class at runtime (`obj.getClass().getMethods()`)
  2. Match argument types exactly — cast or wrap values (e.g. pass Integer not String) so reflection finds the overload
  3. Ensure the runtime classpath has the same library version that declares the method
  4. If the object is generic, use an explicit type so the correct class is reflected

Example fix

// before
$myObj.startsWith(5) // no startsWith(int)
// after
$myObj.startsWith("abc")
Defensive patterns

Strategy: validation

Validate before calling

boolean exists = java.util.Arrays.stream(obj.getClass().getMethods())
    .anyMatch(m -> m.getName().equals(methodName) && m.getParameterCount() == argCount);
if (!exists) throw new IllegalStateException("No method " + methodName + " with " + argCount + " args");

Try / catch

try {
    Value v = expr.evaluate(env, args);
} catch (RuntimeException e) {
    if (e.getMessage().startsWith("Cannot find method ")) {
        // log target class + method, verify signature, correct rule
    } else throw e;
}

Prevention

When it happens

Trigger: Evaluating `$obj.method(args)` where the method name is misspelled, the argument types/count don't match any overload, or the method is not declared on the object's class (or its accessible superclasses/interfaces).

Common situations: Calling Java methods on bound objects from TokensRegex rules with wrong argument types (String vs Integer), calling methods added in a newer JDK/library version than the runtime's classpath, or typos in method names.

Related errors


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

Appendix: source

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

      Method method = null;
      try {
        method = c.getMethod(function, paramTypes);
      } catch (NoSuchMethodException ex) {
        Method[] methods = c.getMethods();
        for (Method m:methods) {
          if (m.getName().equals(function)) {
            Class[] mParamTypes = m.getParameterTypes();
            if (mParamTypes.length == paramTypes.length) {
              boolean compatible = isArgTypesCompatible(paramTypes, mParamTypes);
              if (compatible) {
                method = m;
                break;
              }
            }
          }
        }
        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);
      }

View on GitHub (pinned to 1b7edd19c4)