{"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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L199-L235","documentation":"Thrown as BeanInstantiationException by instantiateClass(Constructor, args) when Constructor.newInstance raises IllegalArgumentException — i.e. the supplied argument values do not match the constructor's declared parameter types (BeanUtils.java:216-218). Note Spring only substitutes primitive defaults for null args; it does not perform type coercion here.","triggerScenarios":"instantiateClass(ctor, args) where args.length > parameterCount is already blocked, but an arg's runtime type is incompatible with the parameter (e.g. passing a String to an int parameter), or a null to a primitive parameter that wasn't defaulted.","commonSituations":"Building args dynamically and passing the wrong type; passing null for a primitive parameter whose default substitution was bypassed; constructor expecting a wrapper type but receiving an incompatible value; mismatched parameter order when collecting args.","solutions":["Align each argument's type with ctor.getParameterTypes() — convert/parse values before calling.","Verify argument count and order against the resolved constructor signature.","For nullable inputs, box primitives or ensure the parameter is a wrapper type so null is acceptable.","Log ctor.getParameterTypes() alongside the args to find the mismatch quickly."],"exampleFix":"// before\nConstructor<User> c = User.class.getDeclaredConstructor(int.class);\nBeanUtils.instantiateClass(c, \"42\"); // String vs int -> IllegalArgumentException\n\n// after\nBeanUtils.instantiateClass(c, Integer.parseInt(\"42\"));","handlingStrategy":"validation","validationCode":"Class<?>[] params = ctor.getParameterTypes();\nif (args.length > params.length) throw new IllegalArgumentException(\"too many args\");\nfor (int i = 0; i < args.length; i++) {\n  if (args[i] != null && !params[i].isPrimitive() && !params[i].isInstance(args[i])) {\n    throw new IllegalArgumentException(\"arg \" + i + \" type mismatch: \" + params[i]);\n  }\n}","typeGuard":"public static boolean argsMatch(Constructor<?> c, Object[] args) {\n  Class<?>[] p = c.getParameterTypes();\n  if (args.length > p.length) return false;\n  for (int i = 0; i < args.length; i++)\n    if (args[i] != null && !p[i].isPrimitive() && !p[i].isInstance(args[i])) return false;\n  return true;\n}","tryCatchPattern":"try { BeanUtils.instantiateClass(ctor, args); }\ncatch (BeanInstantiationException e) {\n  if (e.getCause() instanceof IllegalArgumentException) { /* fix arg types/order */ }\n}","preventionTips":["Match each arg's runtime type to ctor.getParameterTypes().","Validate argument count and order before calling.","Use wrapper types for nullable primitives."],"tags":["spring-beans","beanutils","instantiation","argument-mismatch","reflection"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}