{"record":{"id":"d78549e0b495fe2f","repo":"stanfordnlp/CoreNLP","slug":"cannot-find-method-function-on-object-of-class","errorCode":null,"errorMessage":"Cannot find method ${function} on object of class ${c}","messagePattern":"Cannot find method (.+?) on object of class (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java","lineNumber":1114,"sourceCode":"      Method method = null;\n      try {\n        method = c.getMethod(function, paramTypes);\n      } catch (NoSuchMethodException ex) {\n        Method[] methods = c.getMethods();\n        for (Method m:methods) {\n          if (m.getName().equals(function)) {\n            Class[] mParamTypes = m.getParameterTypes();\n            if (mParamTypes.length == paramTypes.length) {\n              boolean compatible = isArgTypesCompatible(paramTypes, mParamTypes);\n              if (compatible) {\n                method = m;\n                break;\n              }\n            }\n          }\n        }\n        if (method == null) {\n          throw new RuntimeException(\"Cannot find method \" + function + \" on object of class \" + c, ex);\n        }\n      }\n      try {\n        Object res;\n        if (mainObj instanceof MatchResult && method.getName().equals(\"group\")\n                && objs.length == 1 && objs[0] instanceof Integer) {\n          // handle case of calling MatchResult's group(int group) method\n          // this requires casting the mainObj to a MatchResult post Java 8 because\n          // Matcher's toMatchResult() now returns a Matcher$ImmutableMatchResult\n          res = ((MatchResult) mainObj).group((Integer) objs[0]);\n        } else {\n          // handle all other cases\n          res = method.invoke(mainObj, objs);\n        }\n        return new PrimitiveValue<>(function, res);\n      } catch (InvocationTargetException | IllegalAccessException ex) {\n        throw new RuntimeException(\"Cannot evaluate method \" + function + \" on object \" + mainObj, ex);\n      }","sourceCodeStart":1096,"sourceCodeEnd":1132,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java#L1096-L1132","documentation":"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 ...\").","triggerScenarios":"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).","commonSituations":"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.","solutions":["Verify the method name and signature exist on the object's class at runtime (`obj.getClass().getMethods()`)","Match argument types exactly — cast or wrap values (e.g. pass Integer not String) so reflection finds the overload","Ensure the runtime classpath has the same library version that declares the method","If the object is generic, use an explicit type so the correct class is reflected"],"exampleFix":"// before\n$myObj.startsWith(5) // no startsWith(int)\n// after\n$myObj.startsWith(\"abc\")","handlingStrategy":"validation","validationCode":"boolean exists = java.util.Arrays.stream(obj.getClass().getMethods())\n    .anyMatch(m -> m.getName().equals(methodName) && m.getParameterCount() == argCount);\nif (!exists) throw new IllegalStateException(\"No method \" + methodName + \" with \" + argCount + \" args\");","typeGuard":null,"tryCatchPattern":"try {\n    Value v = expr.evaluate(env, args);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Cannot find method \")) {\n        // log target class + method, verify signature, correct rule\n    } else throw e;\n}","preventionTips":["Verify method names and signatures against the object's actual class (getClass().getMethods())","Pass exact parameter types reflection can match (Integer for int params, etc.)","Pin library/JDK versions so methods used in rules exist at runtime"],"tags":["tokensregex","reflection","method-not-found"],"backgroundTag":"method-not-implemented","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}