{"record":{"id":"38ab9cfc5f1c042d","repo":"stanfordnlp/CoreNLP","slug":"cannot-evaluate-method-function-on-object-mai","errorCode":null,"errorMessage":"Cannot evaluate method ${function} on object ${mainObj}","messagePattern":"Cannot evaluate method (.+?) on object (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java","lineNumber":1131,"sourceCode":"        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      }\n    }\n\n    @Override\n    public boolean equals(Object o) {\n      if (this == o) return true;\n      if (!(o instanceof MethodCallExpression)) return false;\n      if (!super.equals(o)) return false;\n\n      MethodCallExpression that = (MethodCallExpression) o;\n\n      if (function != null ? !function.equals(that.function) : that.function != null) return false;\n      if (object != null ? !object.equals(that.object) : that.object != null) return false;\n      if (params != null ? !params.equals(that.params) : that.params != null) return false;\n\n      return true;\n    }\n","sourceCodeStart":1113,"sourceCodeEnd":1149,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java#L1113-L1149","documentation":"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 ...\").","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\n$match.group(3) // match only had 2 groups -> IndexOutOfBoundsException inside\n// after\n$match.group(1)","handlingStrategy":"try-catch","validationCode":"// for MatchResult.group(int): guard index and match success first\nif (match.groupCount() <= idx) throw new IllegalArgumentException(\"No group \" + idx);","typeGuard":null,"tryCatchPattern":"try {\n    Value v = expr.evaluate(env, args);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Cannot evaluate method \") && e.getCause() instanceof InvocationTargetException) {\n        Throwable real = e.getCause().getCause(); // the method's own exception\n    } else throw e;\n}","preventionTips":["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"],"tags":["tokensregex","reflection","method-invocation"],"backgroundTag":"invalid-argument-value","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"}