spring-projects/spring-framework · error · BeanInstantiationException

Illegal arguments for constructor

Error message

Illegal arguments for constructor

What it means

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.

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/BeanUtils.java:217

					if (args[i] == null) {
						Class<?> parameterType = parameterTypes[i];
						argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);
					}
					else {
						argsWithDefaultValues[i] = args[i];
					}
				}
				return ctor.newInstance(argsWithDefaultValues);
			}
		}
		catch (InstantiationException ex) {
			throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
		}
		catch (IllegalAccessException ex) {
			throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
		}
		catch (IllegalArgumentException ex) {
			throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
		}
		catch (InvocationTargetException ex) {
			throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
		}
	}

	/**
	 * Return a resolvable constructor for the provided class, either a primary or single
	 * public constructor with arguments, a single non-public constructor with arguments
	 * or simply a default constructor.
	 * <p>Callers have to be prepared to resolve arguments for the returned constructor's
	 * parameters, if any.
	 * @param clazz the class to check
	 * @throws IllegalStateException in case of no unique constructor found at all
	 * @since 5.3
	 * @see #findPrimaryConstructor
	 */
	@SuppressWarnings("unchecked")

View on GitHub (pinned to e8729d0438)

Solutions

  1. Align each argument's type with ctor.getParameterTypes() — convert/parse values before calling.
  2. Verify argument count and order against the resolved constructor signature.
  3. For nullable inputs, box primitives or ensure the parameter is a wrapper type so null is acceptable.
  4. Log ctor.getParameterTypes() alongside the args to find the mismatch quickly.

Example fix

// before
Constructor<User> c = User.class.getDeclaredConstructor(int.class);
BeanUtils.instantiateClass(c, "42"); // String vs int -> IllegalArgumentException

// after
BeanUtils.instantiateClass(c, Integer.parseInt("42"));
Defensive patterns

Strategy: validation

Validate before calling

Class<?>[] params = ctor.getParameterTypes();
if (args.length > params.length) throw new IllegalArgumentException("too many args");
for (int i = 0; i < args.length; i++) {
  if (args[i] != null && !params[i].isPrimitive() && !params[i].isInstance(args[i])) {
    throw new IllegalArgumentException("arg " + i + " type mismatch: " + params[i]);
  }
}

Type guard

public static boolean argsMatch(Constructor<?> c, Object[] args) {
  Class<?>[] p = c.getParameterTypes();
  if (args.length > p.length) return false;
  for (int i = 0; i < args.length; i++)
    if (args[i] != null && !p[i].isPrimitive() && !p[i].isInstance(args[i])) return false;
  return true;
}

Try / catch

try { BeanUtils.instantiateClass(ctor, args); }
catch (BeanInstantiationException e) {
  if (e.getCause() instanceof IllegalArgumentException) { /* fix arg types/order */ }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/67bc5fcd969a51aa.json. Report an issue: GitHub.