spring-projects/spring-framework · error · BeanInstantiationException
Constructor threw exception
Error message
Constructor threw exception
What it means
Thrown as BeanInstantiationException by instantiateClass(Constructor, args) when the constructor's own body throws; Constructor.newInstance wraps it in InvocationTargetException and Spring unwraps the original target exception as the cause (BeanUtils.java:219-221). The failure originates in user/library constructor code, not in Spring.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/BeanUtils.java:220
}
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")
public static <T> Constructor<T> getResolvableConstructor(Class<T> clazz) {
Constructor<T> ctor = findPrimaryConstructor(clazz);
if (ctor != null) {View on GitHub (pinned to e8729d0438)
Solutions
- Read the cause (and its stack trace) to find the line in the constructor that threw, then fix that code.
- Ensure collaborators/resources the constructor depends on are available before instantiation.
- Supply valid constructor arguments that satisfy the constructor's invariants.
- Move expensive/fallible logic out of the constructor into an @PostConstruct or initializer method.
Example fix
// before
public User(String name) { if (name == null) throw new IllegalArgumentException("name"); }
BeanUtils.instantiateClass(ctor, null); // ctor throws
// after
BeanUtils.instantiateClass(ctor, "Alice"); Defensive patterns
Strategy: try-catch
Validate before calling
// Verify preconditions the constructor enforces before calling // e.g. if (name == null) throw ... -> ensure name != null before instantiate
Type guard
public static boolean ctorArgsSatisfyInvariants(Constructor<?> c, Object[] args) {
// project-specific: check non-null/type constraints derived from ctor Javadoc/validation
return Arrays.stream(args).noneMatch(Objects::isNull) || c.getParameterCount() == 0;
} Try / catch
try { BeanUtils.instantiateClass(ctor, args); }
catch (BeanInstantiationException e) {
Throwable real = e.getCause(); // the constructor's own exception; fix its source
} Prevention
- Inspect the unwrapped cause to find the failing line in the constructor.
- Provide valid arguments and available collaborators before instantiation.
- Move fallible logic out of constructors into @PostConstruct methods.
When it happens
Trigger: instantiateClass(ctor, args) where the constructor executes code that throws (NullPointerException, IllegalStateException, IllegalArgumentException, a domain exception, etc.). The thrown exception is propagated verbatim as the cause.
Common situations: A bean constructor validating arguments and throwing; field initialization that fails (e.g. loading a resource that is missing); constructor calling a collaborator that is null/unconfigured; defensive constructors rejecting bad state.
Related errors
- Specified class is an interface
- Is it an abstract class?
- Is the constructor accessible?
- No default constructor found
- Unresolvable class definition
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/b30b9b8d975a2134.json.
Report an issue: GitHub.