{"record":{"id":"38f07cb5b3f5e69a","repo":"elastic/elasticsearch","slug":"cannot-instantiate-an-object-of-classname","errorCode":null,"errorMessage":"Cannot instantiate an object of {className}","messagePattern":"Cannot instantiate an object of (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/InstantiatingObjectParser.java","lineNumber":228,"sourceCode":"        }\n\n        private Value buildInstance(Object[] args, Context context) {\n            if (constructor == null) {\n                throw new IllegalArgumentException(\n                    \"InstantiatingObjectParser for type \" + valueClass.getName() + \" has to be finalized \" + \"before the first use\"\n                );\n            }\n            try {\n                if (constructor.getParameterCount() != args.length) {\n                    Object[] newArgs = new Object[args.length + 1];\n                    System.arraycopy(args, 0, newArgs, 1, args.length);\n                    newArgs[0] = context;\n                    return constructor.newInstance(newArgs);\n                } else {\n                    return constructor.newInstance(args);\n                }\n            } catch (Exception ex) {\n                throw new IllegalArgumentException(\"Cannot instantiate an object of \" + valueClass.getName(), ex);\n            }\n        }\n    }\n\n    private final ConstructingObjectParser<Value, Context> constructingObjectParser;\n\n    private InstantiatingObjectParser(ConstructingObjectParser<Value, Context> constructingObjectParser) {\n        this.constructingObjectParser = constructingObjectParser;\n    }\n\n    @Override\n    public Value parse(XContentParser parser, Context context) throws IOException {\n        return constructingObjectParser.parse(parser, context);\n    }\n\n    @Override\n    public Value apply(XContentParser xContentParser, Context context) {\n        return constructingObjectParser.apply(xContentParser, context);","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/InstantiatingObjectParser.java#L210-L246","documentation":"InstantiatingObjectParser.buildInstance reflectively invokes the target constructor via Constructor.newInstance. Any failure (wrong arg count/types, inaccessible constructor, or the constructor itself throwing) is wrapped as IllegalArgumentException naming the target class. The branch handles two arity paths: if constructor parameter count differs from args length, it prepends the context as the first argument (context-injecting constructor).","triggerScenarios":"The declared constructor is not accessible (non-public / non-setAccessible), the builder supplied args whose types don't match the constructor signature, the constructor throws on invalid combinations, or the context-prepending arity assumption is wrong for the chosen constructor.","commonSituations":"Builder/declaration mismatch where constructorArg order or types diverge from the actual constructor. Forgetting to call finalize() (caught separately) or using a non-public constructor without making it accessible. Arg-count mismatch between declared constructor args and the constructor that accepts a context.","solutions":["Inspect the cause: InvocationTargetException unwraps to the constructor's real exception; IllegalAccessException means accessibility — fix by making the constructor accessible or public.","Verify the order and types of constructorArg() declarations exactly match the chosen constructor's parameter list (and that the context-accepting overload, if any, is the one registered).","Ensure the valueClass and constructor registered in the Builder match the args the ConstructingObjectParser collects."],"exampleFix":"// before: constructor is package-private and parser cannot access it\nMyClass(String a, int b) // package-private\n\n// after: make it accessible to InstantiatingObjectParser\npublic MyClass(String a, int b)","handlingStrategy":"validation","validationCode":"// verify constructor accessibility and arg match before registering\nConstructor<?> c = valueClass.getDeclaredConstructor(argTypes);\nif (!Modifier.isPublic(c.getModifiers()) && !c.canAccess(null)) c.setAccessible(true);","typeGuard":null,"tryCatchPattern":"try {\n    Value v = iop.parse(parser, ctx);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Cannot instantiate an object of \")) {\n        Throwable root = e.getCause(); // InvocationTargetException -> unwrap getTargetException\n    }\n}","preventionTips":["Make target constructors public (or setAccessible) so reflective instantiation succeeds.","Keep constructorArg() declaration order/types in lockstep with the constructor signature; pin with a unit test.","If using the context-prepending overload, confirm its arity is exactly declared-args + 1 with context as param[0]."],"tags":["xcontent","instantiating-object-parser","reflection","construction"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}