{"id":"bcac2ac8d3664bab","repo":"spring-projects/spring-framework","slug":"no-default-constructor-found","errorCode":null,"errorMessage":"No default constructor found","messagePattern":"No default constructor found","errorType":"exception","errorClass":"BeanInstantiationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/BeanUtils.java","lineNumber":143,"sourceCode":"\t * primary/default constructor was found, a {@link NoClassDefFoundError}\n\t * or other {@link LinkageError} in case of an unresolvable class definition\n\t * (for example, due to a missing dependency at runtime), or an exception thrown\n\t * from the constructor invocation itself.\n\t * @see Constructor#newInstance\n\t */\n\tpublic static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {\n\t\tAssert.notNull(clazz, \"Class must not be null\");\n\t\tif (clazz.isInterface()) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Specified class is an interface\");\n\t\t}\n\t\tConstructor<T> ctor;\n\t\ttry {\n\t\t\tctor = clazz.getDeclaredConstructor();\n\t\t}\n\t\tcatch (NoSuchMethodException ex) {\n\t\t\tctor = findPrimaryConstructor(clazz);\n\t\t\tif (ctor == null) {\n\t\t\t\tthrow new BeanInstantiationException(clazz, \"No default constructor found\", ex);\n\t\t\t}\n\t\t}\n\t\tcatch (LinkageError err) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Unresolvable class definition\", err);\n\t\t}\n\t\treturn instantiateClass(ctor);\n\t}\n\n\t/**\n\t * Instantiate a class using its no-arg constructor and return the new instance\n\t * as the specified assignable type.\n\t * <p>Useful in cases where the type of the class to instantiate (clazz) is not\n\t * available, but the type desired (assignableTo) is known.\n\t * <p>Note that this method tries to set the constructor accessible if given a\n\t * non-accessible (that is, non-public) constructor.\n\t * @param clazz class to instantiate\n\t * @param assignableTo type that clazz must be assignableTo\n\t * @return the new instance","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L125-L161","documentation":"Thrown as BeanInstantiationException by instantiateClass(Class) when clazz.getDeclaredConstructor() (no-arg) raises NoSuchMethodException and findPrimaryConstructor(clazz) also returns null. This means the class has neither a no-arg constructor nor a resolvable primary/canonical constructor (Kotlin primary or Java record canonical), so Spring cannot pick one (BeanUtils.java:136-145).","triggerScenarios":"instantiateClass(Foo.class) on a Java class whose only constructors take arguments and that is neither a Kotlin class with a primary constructor nor a record. The NoSuchMethodException is retained as the cause.","commonSituations":"Refactoring a class to add a required-args constructor and removing the no-arg one; instantiating an immutable/value type by class only; integration code assuming a default constructor exists.","solutions":["Add a public no-arg constructor to the class.","Resolve the constructor yourself with getDeclaredConstructor(argTypes...) and call instantiateClass(ctor, args).","Use getResolvableConstructor to obtain a single unique constructor and supply matching arguments.","For Kotlin classes ensure the kotlin-reflect dependency is present so the primary constructor can be found."],"exampleFix":"// before\npublic class User { public User(String name) {} }\nBeanUtils.instantiateClass(User.class); // no default ctor\n\n// after\nConstructor<User> c = User.class.getDeclaredConstructor(String.class);\nBeanUtils.instantiateClass(c, \"Alice\");","handlingStrategy":"validation","validationCode":"Constructor<?> def;\ntry { def = clazz.getDeclaredConstructor(); }\ncatch (NoSuchMethodException e) { def = null; }\nif (def == null && BeanUtils.findPrimaryConstructor(clazz) == null) {\n  // resolve an explicit constructor and pass args instead\n}","typeGuard":"public static boolean hasDefaultOrPrimaryCtor(Class<?> c) {\n  try { c.getDeclaredConstructor(); return true; }\n  catch (NoSuchMethodException e) { return BeanUtils.findPrimaryConstructor(c) != null; }\n}","tryCatchPattern":"try { BeanUtils.instantiateClass(clazz); }\ncatch (BeanInstantiationException e) {\n  Constructor<?> c = clazz.getDeclaredConstructor(String.class);\n  BeanUtils.instantiateClass(c, arg);\n}","preventionTips":["Add a no-arg constructor when you add required-args constructors.","For Kotlin, keep kotlin-reflect on the classpath for primary-constructor detection.","Resolve constructors explicitly with getDeclaredConstructor(types) when args are known."],"tags":["spring-beans","beanutils","instantiation","no-default-constructor","kotlin"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}