{"record":{"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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L125-L161","documentation":"Thrown as BeanInstantiationException by instantiateClass(Class) at BeanUtils.java:143 when getDeclaredConstructor() (no-arg) throws NoSuchMethodException AND findPrimaryConstructor(clazz) also returns null. So the class has neither a default no-arg constructor nor a discoverable primary/canonical constructor (Kotlin primary or record canonical). The NoSuchMethodException is attached as the cause.","triggerScenarios":"Instantiating a Java class that declares only parameterized constructors (no no-arg ctor) and is neither a Kotlin class with a primary constructor nor a Java record. getDeclaredConstructor() fails, findPrimaryConstructor returns null, and the exception is raised.","commonSituations":"@ConfigurationProperties/value objects that only have an all-args constructor; domain classes with required fields and no default ctor; instantiating a class that only has constructors with @Autowired; legacy POJOs missing the implicit default constructor after adding a custom constructor.","solutions":["Add a no-arg constructor to the class (optionally alongside others).","Use instantiateClass(Constructor, args) with the specific constructor and supply arguments.","For Java records use the canonical constructor (findPrimaryConstructor handles it) — make sure Kotlin reflect is present if mixing types.","Use getResolvableConstructor to pick the right constructor explicitly."],"exampleFix":"// before\npublic class User {\n    public User(String name, int age) { ... } // only ctor\n}\nBeanUtils.instantiateClass(User.class);\n\n// after\npublic class User {\n    public User() {}\n    public User(String name, int age) { ... }\n}","handlingStrategy":"validation","validationCode":"// Check for a usable no-arg or primary constructor first\nConstructor<?> c;\ntry { c = clazz.getDeclaredConstructor(); }\ncatch (NoSuchMethodException e) {\n    c = BeanUtils.findPrimaryConstructor(clazz);\n}\nif (c == null) {\n    // supply explicit constructor + args instead\n}","typeGuard":"static boolean hasDefaultOrPrimary(Class<?> c) {\n    try { c.getDeclaredConstructor(); return true; }\n    catch (NoSuchMethodException e) { return BeanUtils.findPrimaryConstructor(c) != null; }\n}","tryCatchPattern":"try {\n    BeanUtils.instantiateClass(clazz);\n} catch (BeanInstantiationException ex) {\n    if (ex.getCause() instanceof NoSuchMethodException) {\n        Constructor<?> c = clazz.getDeclaredConstructors()[0];\n        Object bean = BeanUtils.instantiateClass(c, suppliedArgs);\n    } else throw ex;\n}","preventionTips":["Add a no-arg constructor to classes that will be instantiated by class.","Use instantiateClass(Constructor, args) when only parameterized constructors exist.","For Java records rely on the canonical constructor (handled by findPrimaryConstructor).","Validate the class has a default or primary constructor before instantiation."],"tags":["spring-beans","beanutils","instantiation","constructor","no-arg"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}