{"record":{"id":"dd52d31edc05b855","repo":"json-path/JsonPath","slug":"unable-to-find-no-arg-ctr-for-classname","errorCode":null,"errorMessage":"Unable to find no-arg ctr for \" + className","messagePattern":"Unable to find no-arg ctr for \" \\+ className","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java","lineNumber":340,"sourceCode":"     * @throws MappingException if no-arg public constructor is not there \r\n     */\r\n    private Object newNoArgInstance(Class<?> targetType) throws MappingException {\r\n        if (targetType.isInterface()) {\r\n            return null;\r\n        } else {\r\n            for (Constructor<?> ctr : targetType.getConstructors()) {\r\n                if (ctr.getParameterCount() == 0) {\r\n                    try {\r\n                        return ctr.newInstance();\r\n                    } catch (ReflectiveOperationException e) {\r\n                        throw new MappingException(e);\r\n                    } catch (IllegalArgumentException e) {\r\n                        // never happens\r\n                    }\r\n                }\r\n            }\r\n            String className = targetType.getSimpleName();\r\n            throw new MappingException(\"Unable to find no-arg ctr for \" + className);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Locates optional API method on the supplied JSON-B API implementation class with\r\n     * the supplied name and parameter types. Searches the superclasses up to\r\n     * {@code Object}, but ignores interfaces and default interface methods. Returns\r\n     * {@code null} if no {@code Method} can be found. \r\n     *\r\n     * @param clazz the implementation class to reflect upon\r\n     * @param name the name of the method\r\n     * @param paramTypes the parameter types of the method\r\n     * @return the {@code Method} reference, or {@code null} if none found\r\n     */\r\n    private Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {\r\n        while (clazz != null && !clazz.isInterface()) {\r\n            for (Method method : clazz.getDeclaredMethods()) {\r\n                final int mods = method.getModifiers();\r","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/spi/mapper/JakartaMappingProvider.java#L322-L358","documentation":"newNoArgInstance() reflectively instantiates a concrete class found while building a target collection (and other databind targets) by scanning its public constructors for a zero-argument one. If the class has no public no-arg constructor, or only interfaces were found, it throws MappingException \"Unable to find no-arg ctr for <ClassName>\". JSON-P/JSON-B style databinding requires bean-style types that can be created empty and then populated.","triggerScenarios":"Mapping a JSON array into a concrete collection class lacking a public no-arg constructor (e.g. a custom ImmutableList extends AbstractList with only (Collection) ctor), or mapping to a POJO element/collection type without a default constructor, via read(path, Type.class) or TypeRef on JakartaMappingProvider.","commonSituations":"Immutable/value classes and builder-pattern DTOs; Lombok @Builder without @NoArgsConstructor; inner (non-static) classes whose constructors require an enclosing instance; classes with only parameterized constructors added after a refactor.","solutions":["Add a public no-arg constructor to the target class (and standard getters/setters so databinding can populate it).","Use a type the provider can construct: java.util.LinkedList/ArrayList/LinkedHashSet instead of a custom collection class.","If you cannot modify the class, read into Map<String,Object> / List<Object> and construct your type manually.","For Lombok, add @NoArgsConstructor(access = AccessLevel.PUBLIC) alongside @Builder."],"exampleFix":"// before\n@Builder\nclass Book { private String title; } // MappingException: Unable to find no-arg ctr for Book\n// after\n@Builder\n@NoArgsConstructor\n@AllArgsConstructor\nclass Book { private String title; }","handlingStrategy":"validation","validationCode":"static boolean databindable(Class<?> t) {\n    try {\n        t.getConstructor();\n        return true;\n    } catch (NoSuchMethodException e) {\n        return false;\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return jsonPath.read(path, MyDto.class);\n} catch (MappingException e) {\n    return objectMapper.convertValue(jsonPath.read(path, Map.class), MyDto.class);\n}","preventionTips":["Give every databind target a public no-arg constructor and bean setters.","With Lombok @Builder, always add @NoArgsConstructor and @AllArgsConstructor.","Avoid non-static inner classes as mapping targets.","Keep target classes top-level and mutable for provider compatibility."],"tags":["json","reflection","databind","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}