{"record":{"id":"9146f818b5df5d4c","repo":"java-native-access/jna","slug":"can-t-create-an-instance-of-klass-requires-a-public-no-arg","errorCode":null,"errorMessage":"Can't create an instance of <klass>, requires a public no-arg constructor: <cause>","messagePattern":"Can't create an instance of <klass>, requires a public no-arg constructor: <cause>","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Klass.java","lineNumber":52,"sourceCode":"    /**\n     * Create a new instance for the given {@code klass}. Runtime exceptions\n     * thrown from the constructor are rethrown, all other exceptions\n     * generated from the reflective call are wrapped into a\n     * {@link java.lang.IllegalArgumentException} and rethrown.\n     *\n     * @param klass desired class to instantiate\n     * @return the new instance\n     * @throws IllegalArgumentException if the instantiation fails\n     * @throws RuntimeException if the constructor for {@code klass} throws\n     *         a runtime exception\n     */\n    public static <T> T newInstance(Class<T> klass) {\n        try {\n            return klass.getDeclaredConstructor().newInstance();\n        } catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException e) {\n            String msg = \"Can't create an instance of \" + klass\n                    + \", requires a public no-arg constructor: \" + e;\n            throw new IllegalArgumentException(msg, e);\n        } catch (InvocationTargetException e) {\n            if (e.getCause() instanceof RuntimeException) {\n                throw (RuntimeException) e.getCause();\n            } else {\n                String msg = \"Can't create an instance of \" + klass\n                        + \", requires a public no-arg constructor: \" + e;\n                throw new IllegalArgumentException(msg, e);\n            }\n        }\n    }\n}\n","sourceCodeStart":34,"sourceCodeEnd":64,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Klass.java#L34-L64","documentation":"Klass.newInstance wraps reflection failures (IllegalAccess, NoSuchMethod, InstantiationException, etc.) into this IllegalArgumentException when it cannot instantiate the class via its public no-arg constructor. JNA uses it internally to create instances of Structure, Callback and similar types.","triggerScenarios":"Calling Klass.newInstance(klass) on a class with no declared no-arg constructor, a non-public no-arg constructor, an abstract class, or an interface; a SecurityException from the SecurityManager.","commonSituations":"Nested (non-static inner) classes whose implicit constructor needs an enclosing instance; classes with only parameterized constructors; anonymous/local classes; instantiating interfaces by mistake.","solutions":["Add a public no-arg constructor to the class (make it static if nested).","Use the overload that accepts a preallocated instance, e.g. Structure's use of newInstance with a target object.","If the class is abstract or an interface, instantiate a concrete subclass instead.","Check the wrapped cause `e` in the message for the exact reflection failure."],"exampleFix":"// before\nclass Point { Point(int x, int y) {} }\n// after\nclass Point { public Point() {} public Point(int x, int y) {} }","handlingStrategy":"validation","validationCode":"static boolean hasPublicNoArgCtor(Class<?> k) {\n  try { k.getDeclaredConstructor(); return java.lang.reflect.Modifier.isPublic(k.getModifiers()) && !java.lang.reflect.Modifier.isAbstract(k.getModifiers()); }\n  catch (NoSuchMethodException e) { return false; }\n}","typeGuard":"static <T> T safeNewInstance(Class<T> k) {\n  if (!hasPublicNoArgCtor(k)) throw new IllegalArgumentException(k + \" lacks a public no-arg ctor\");\n  return com.sun.jna.Klass.newInstance(k);\n}","tryCatchPattern":"try {\n  T t = Klass.newInstance(klass);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"requires a public no-arg constructor\")) {\n    throw new IllegalStateException(\"Make \" + klass.getSimpleName() + \" static/concrete with a public no-arg ctor\", e.getCause());\n  }\n  throw e;\n}","preventionTips":["Give Structure/mapped classes a public no-arg constructor.","Make nested mapping classes static.","Never map abstract classes or interfaces for instantiation.","Read the cause appended to the message to pinpoint the reflection failure."],"tags":["jna","reflection","instantiation","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}