elastic/elasticsearch · error · IllegalArgumentException
Cannot instantiate an object of {className}
Error message
Cannot instantiate an object of {className} What it means
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).
Source
Thrown at libs/x-content/src/main/java/org/elasticsearch/xcontent/InstantiatingObjectParser.java:228
}
private Value buildInstance(Object[] args, Context context) {
if (constructor == null) {
throw new IllegalArgumentException(
"InstantiatingObjectParser for type " + valueClass.getName() + " has to be finalized " + "before the first use"
);
}
try {
if (constructor.getParameterCount() != args.length) {
Object[] newArgs = new Object[args.length + 1];
System.arraycopy(args, 0, newArgs, 1, args.length);
newArgs[0] = context;
return constructor.newInstance(newArgs);
} else {
return constructor.newInstance(args);
}
} catch (Exception ex) {
throw new IllegalArgumentException("Cannot instantiate an object of " + valueClass.getName(), ex);
}
}
}
private final ConstructingObjectParser<Value, Context> constructingObjectParser;
private InstantiatingObjectParser(ConstructingObjectParser<Value, Context> constructingObjectParser) {
this.constructingObjectParser = constructingObjectParser;
}
@Override
public Value parse(XContentParser parser, Context context) throws IOException {
return constructingObjectParser.parse(parser, context);
}
@Override
public Value apply(XContentParser xContentParser, Context context) {
return constructingObjectParser.apply(xContentParser, context);View on GitHub (pinned to db6a809a66)
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.
Example fix
// before: constructor is package-private and parser cannot access it MyClass(String a, int b) // package-private // after: make it accessible to InstantiatingObjectParser public MyClass(String a, int b)
Defensive patterns
Strategy: validation
Validate before calling
// verify constructor accessibility and arg match before registering Constructor<?> c = valueClass.getDeclaredConstructor(argTypes); if (!Modifier.isPublic(c.getModifiers()) && !c.canAccess(null)) c.setAccessible(true);
Try / catch
try {
Value v = iop.parse(parser, ctx);
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Cannot instantiate an object of ")) {
Throwable root = e.getCause(); // InvocationTargetException -> unwrap getTargetException
}
} Prevention
- 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].
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Module {module.getName()} does not contain qualified exports
- Error occurred when inspecting class: {}; SerializedLambda r
- Error occurred when inspecting class: {}
- Proxy fallback only supports instance method references; no
- No constructor found on {} with parameter types {}
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/38f07cb5b3f5e69a.
Report an issue: GitHub.