{"record":{"id":"345850fe5c186ed6","repo":"stanfordnlp/CoreNLP","slug":"cannot-instantiate-c","errorCode":null,"errorMessage":"Cannot instantiate ${c}","messagePattern":"Cannot instantiate (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java","lineNumber":999,"sourceCode":"            return new PrimitiveValue<>(function, obj);\n          }\n        }\n        try {\n          Constructor constructor = null;\n          try {\n            constructor = c.getConstructor(paramTypes);\n          } catch (NoSuchMethodException ex) {\n            Constructor[] constructors = c.getConstructors();\n            for (Constructor cons:constructors) {\n              Class[] consParamTypes = cons.getParameterTypes();\n              boolean compatible = isArgTypesCompatible(paramTypes, consParamTypes);\n              if (compatible) {\n                constructor = cons;\n                break;\n              }\n            }\n            if (constructor == null) {\n              throw new RuntimeException(\"Cannot instantiate \" + c, ex);\n            }\n          }\n          Object obj = constructor.newInstance(objs);\n          return new PrimitiveValue<>(function, obj);\n        } catch (InvocationTargetException | InstantiationException | IllegalAccessException ex) {\n          throw new RuntimeException(\"Cannot instantiate \" + c, ex);\n        }\n      } else {\n        throw new UnsupportedOperationException(\"Unsupported function value \" + funcValue);\n      }\n    }\n\n    @Override\n    public boolean equals(Object o) {\n      if (this == o) return true;\n      if (!(o instanceof FunctionCallExpression)) return false;\n\n      FunctionCallExpression that = (FunctionCallExpression) o;","sourceCodeStart":981,"sourceCodeEnd":1017,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java#L981-L1017","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass arguments that exactly match one of the class's public constructor signatures","Ensure the target class is public and has a usable constructor","Instantiate the object in Java code and bind it into the Env instead of constructing it from the rule","Check the wrapped cause (`ex`) in the stack trace for the real failure (NoSuchMethodException vs probe exception)"],"exampleFix":"// before\n$java.lang.String(1, 2, 3) // no such constructor\n// after\n$java.lang.String(\"abc\")","handlingStrategy":"validation","validationCode":"Class<?> c = Class.forName(\"com.example.Foo\");\nboolean hasCtor = java.util.Arrays.stream(c.getConstructors())\n    .anyMatch(k -> k.getParameterCount() == args.length);","typeGuard":null,"tryCatchPattern":"try {\n    Value v = expr.evaluate(env, args);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Cannot instantiate \")) {\n        Throwable cause = e.getCause(); // real reflection failure\n        // fall back to constructing the object in Java code\n    } else throw e;\n}","preventionTips":["Check constructor signatures of target classes before using them in rules","Only use public classes with public constructors in expression class-calls","Prefer binding pre-built instances in the Env over reflective construction"],"tags":["tokensregex","reflection","instantiation"],"backgroundTag":"invalid-constructor-argument","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"}