{"record":{"id":"67bc5fcd969a51aa","repo":"spring-projects/spring-framework","slug":"illegal-arguments-for-constructor","errorCode":null,"errorMessage":"Illegal arguments for constructor","messagePattern":"Illegal arguments for constructor","errorType":"exception","errorClass":"BeanInstantiationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/BeanUtils.java","lineNumber":217,"sourceCode":"\t\t\t\t\tif (args[i] == null) {\n\t\t\t\t\t\tClass<?> parameterType = parameterTypes[i];\n\t\t\t\t\t\targsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\targsWithDefaultValues[i] = args[i];\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn ctor.newInstance(argsWithDefaultValues);\n\t\t\t}\n\t\t}\n\t\tcatch (InstantiationException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Is it an abstract class?\", ex);\n\t\t}\n\t\tcatch (IllegalAccessException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Is the constructor accessible?\", ex);\n\t\t}\n\t\tcatch (IllegalArgumentException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Illegal arguments for constructor\", ex);\n\t\t}\n\t\tcatch (InvocationTargetException ex) {\n\t\t\tthrow new BeanInstantiationException(ctor, \"Constructor threw exception\", ex.getTargetException());\n\t\t}\n\t}\n\n\t/**\n\t * Return a resolvable constructor for the provided class, either a primary or single\n\t * public constructor with arguments, a single non-public constructor with arguments\n\t * or simply a default constructor.\n\t * <p>Callers have to be prepared to resolve arguments for the returned constructor's\n\t * parameters, if any.\n\t * @param clazz the class to check\n\t * @throws IllegalStateException in case of no unique constructor found at all\n\t * @since 5.3\n\t * @see #findPrimaryConstructor\n\t */\n\t@SuppressWarnings(\"unchecked\")","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L199-L235","documentation":"Thrown as BeanInstantiationException by instantiateClass(Constructor, args) at BeanUtils.java:217 when Constructor.newInstance raises IllegalArgumentException. This means the supplied argument array does not match the constructor's parameter types (wrong count, wrong type, or a primitive parameter given an incompatible/wrong wrapper). The pre-check at line 192 only guards the upper bound (args.length <= paramCount); it does not type-check.","triggerScenarios":"Passing args whose runtime types are not assignable to the constructor's parameter types; passing a null for a primitive parameter where DEFAULT_TYPE_VALUES lookup did not apply (e.g. wrong index); passing more/wider args than expected after the length pre-check slipped through; boxed type mismatch like passing a Long to an int param.","commonSituations":"Programmatic bean instantiation where arguments are built dynamically; reflection-based mappers; converting user input to constructor args without type coercion; Kotlin default-arg handling when null is passed for a non-optional, non-primitive parameter that the framework mis-counts.","solutions":["Compare ctor.getParameterTypes() against your args' classes element-by-element before calling.","Coerce each arg to its exact parameter type (convert Long->int, String->enum, etc.).","Use MethodParameter / ResolvableType to resolve generics when types are parameterized.","For nullable/primitive params, follow the same rule as BeanUtils: substitute DEFAULT_TYPE_VALUES for primitive nulls."],"exampleFix":"// before\nConstructor<User> c = User.class.getConstructor(String.class, int.class);\nUser u = BeanUtils.instantiateClass(c, \"bob\", 30L); // Long into int -> IAE\n\n// after\nUser u = BeanUtils.instantiateClass(c, \"bob\", (int) 30L);","handlingStrategy":"validation","validationCode":"Constructor<?> c = ...;\nClass<?>[] pts = c.getParameterTypes();\nif (args.length <= pts.length) {\n    for (int i = 0; i < args.length; i++) {\n        if (args[i] != null && !pts[i].isPrimitive()\n                && !pts[i].isAssignableFrom(args[i].getClass())) {\n            args[i] = conversionService.convert(args[i], pts[i]);\n        }\n    }\n}\nBeanUtils.instantiateClass(c, args);","typeGuard":"static boolean argsMatch(Constructor<?> c, Object[] args) {\n    Class<?>[] pts = c.getParameterTypes();\n    if (args.length > pts.length) return false;\n    for (int i = 0; i < args.length; i++) {\n        if (args[i] == null) continue;\n        Class<?> need = pts[i].isPrimitive() ? wrap(pts[i]) : pts[i];\n        if (!need.isAssignableFrom(args[i].getClass())) return false;\n    }\n    return true;\n}","tryCatchPattern":"try {\n    BeanUtils.instantiateClass(ctor, args);\n} catch (BeanInstantiationException ex) {\n    if (ex.getCause() instanceof IllegalArgumentException) {\n        // coerce args to ctor.getParameterTypes() and retry once\n    } else throw ex;\n}","preventionTips":["Match each arg's runtime type to ctor.getParameterTypes() before invocation.","Convert numeric/string args to the exact parameter type (long->int, String->enum).","Handle primitives explicitly: substitute defaults for null primitive args.","Prefer typed factory methods when arg types are dynamic."],"tags":["spring-beans","beanutils","instantiation","constructor","argument-mismatch","reflection"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}