{"id":"b30b9b8d975a2134","repo":"spring-projects/spring-framework","slug":"constructor-threw-exception","errorCode":null,"errorMessage":"Constructor threw exception","messagePattern":"Constructor threw exception","errorType":"exception","errorClass":"BeanInstantiationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/BeanUtils.java","lineNumber":220,"sourceCode":"\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\")\n\tpublic static <T> Constructor<T> getResolvableConstructor(Class<T> clazz) {\n\t\tConstructor<T> ctor = findPrimaryConstructor(clazz);\n\t\tif (ctor != null) {","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L202-L238","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic User(String name) { if (name == null) throw new IllegalArgumentException(\"name\"); }\nBeanUtils.instantiateClass(ctor, null); // ctor throws\n\n// after\nBeanUtils.instantiateClass(ctor, \"Alice\");","handlingStrategy":"try-catch","validationCode":"// Verify preconditions the constructor enforces before calling\n// e.g. if (name == null) throw ... -> ensure name != null before instantiate","typeGuard":"public static boolean ctorArgsSatisfyInvariants(Constructor<?> c, Object[] args) {\n  // project-specific: check non-null/type constraints derived from ctor Javadoc/validation\n  return Arrays.stream(args).noneMatch(Objects::isNull) || c.getParameterCount() == 0;\n}","tryCatchPattern":"try { BeanUtils.instantiateClass(ctor, args); }\ncatch (BeanInstantiationException e) {\n  Throwable real = e.getCause(); // the constructor's own exception; fix its source\n}","preventionTips":["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."],"tags":["spring-beans","beanutils","instantiation","constructor-exception","propagation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}