{"record":{"id":"2b9fbbdd60029129","repo":"java-native-access/jna","slug":"can-t-instantiate-type","errorCode":null,"errorMessage":"Can't instantiate \" + type","messagePattern":"Can't instantiate \" \\+ type","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Structure.java","lineNumber":1952,"sourceCode":"     * @param type desired Structure type\n     * @param init initial memory\n     * @return the new instance\n     * @throws IllegalArgumentException if the instantiation fails\n     */\n    public static <T extends Structure> T newInstance(Class<T> type, Pointer init) throws IllegalArgumentException {\n        try {\n            Constructor<T> ctor = getPointerConstructor(type);\n            if (ctor != null) {\n                return ctor.newInstance(init);\n            }\n            // Not defined, fall back to the default\n        }\n        catch(SecurityException e) {\n            // Might as well try the fallback\n        }\n        catch(InstantiationException e) {\n            String msg = \"Can't instantiate \" + type;\n            throw new IllegalArgumentException(msg, e);\n        }\n        catch(IllegalAccessException e) {\n            String msg = \"Instantiation of \" + type + \" (Pointer) not allowed, is it public?\";\n            throw new IllegalArgumentException(msg, e);\n        }\n        catch(InvocationTargetException e) {\n            String msg = \"Exception thrown while instantiating an instance of \" + type;\n            throw new IllegalArgumentException(msg, e);\n        }\n        T s = newInstance(type);\n        if (init != PLACEHOLDER_MEMORY) {\n            s.useMemory(init);\n        }\n        return s;\n    }\n\n    /**\n     * Create a new Structure instance of the given type","sourceCodeStart":1934,"sourceCodeEnd":1970,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Structure.java#L1934-L1970","documentation":"Structure.newInstance(Class, Pointer) reflectively invokes the structure's (Pointer) constructor. If that constructor is abstract at the class level or otherwise cannot be instantiated (InstantiationException), JNA rethrows it as IllegalArgumentException with \"Can't instantiate <type>\". InstantiationException from Constructor.newInstance means the class is abstract or an interface/annotation type.","triggerScenarios":"Calling Structure.newInstance() / newInstance(type, pointer) (or triggering nested-structure field initialization) where `type` is an abstract Structure subclass, an interface extending Structure, or a non-instantiable class lacking a concrete implementation.","commonSituations":"Passing an abstract base Structure class instead of the concrete subclass to a library function; registering a Structure interface as a return type; refactoring that made a previously concrete Structure abstract while callers still instantiate it reflectively.","solutions":["Use a concrete (non-abstract) Structure subclass; check that `type` is neither abstract nor an interface.","Implement the abstract class as an anonymous or concrete subclass where it is used.","Fix the underlying definition: if the abstract class is the intended field type, declare the field with the concrete implementation instead.","Catch IllegalArgumentException and inspect the cause (InstantiationException) to identify which class is abstract."],"exampleFix":"// before\npublic abstract class Msg extends Structure { ... }\nMsg m = Structure.newInstance(Msg.class, ptr);\n// after\npublic class MsgImpl extends Msg { ... }\nMsg m = Structure.newInstance(MsgImpl.class, ptr);","handlingStrategy":"type-guard","validationCode":"static <T extends Structure> void requireConcrete(Class<T> type) {\n    int mods = type.getModifiers();\n    if (java.lang.reflect.Modifier.isAbstract(mods) || type.isInterface())\n        throw new IllegalStateException(type + \" is abstract/interface; pass a concrete Structure subclass\");\n}","typeGuard":"static boolean isInstantiableStructure(Class<?> c) {\n    int m = c.getModifiers();\n    return Structure.class.isAssignableFrom(c)\n        && !java.lang.reflect.Modifier.isAbstract(m)\n        && !c.isInterface()\n        && !java.lang.reflect.Modifier.isPrivate(m);\n}","tryCatchPattern":"try {\n    MyStruct s = Structure.newInstance(type, pointer);\n} catch (IllegalArgumentException e) {\n    if (e.getCause() instanceof InstantiationException) {\n        throw new IllegalStateException(type + \" is abstract; use a concrete subclass\", e);\n    }\n    throw e;\n}","preventionTips":["Never pass abstract Structure base classes or interfaces to newInstance or library functions expecting a Structure type","Keep concrete implementations even for shared base structs (anonymous subclasses are fine)","Run reflection checks in tests: assert each Structure class used for callbacks/returns is concrete","Watch for refactors that add `abstract` to a Structure still instantiated via JNA"],"tags":["jna","structure","reflection","abstract-class","instantiation"],"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"}