java-native-access/jna · error · IllegalArgumentException

Can't instantiate " + type

Error message

Can't instantiate " + type

What it means

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.

Source

Thrown at src/com/sun/jna/Structure.java:1952

     * @param type desired Structure type
     * @param init initial memory
     * @return the new instance
     * @throws IllegalArgumentException if the instantiation fails
     */
    public static <T extends Structure> T newInstance(Class<T> type, Pointer init) throws IllegalArgumentException {
        try {
            Constructor<T> ctor = getPointerConstructor(type);
            if (ctor != null) {
                return ctor.newInstance(init);
            }
            // Not defined, fall back to the default
        }
        catch(SecurityException e) {
            // Might as well try the fallback
        }
        catch(InstantiationException e) {
            String msg = "Can't instantiate " + type;
            throw new IllegalArgumentException(msg, e);
        }
        catch(IllegalAccessException e) {
            String msg = "Instantiation of " + type + " (Pointer) not allowed, is it public?";
            throw new IllegalArgumentException(msg, e);
        }
        catch(InvocationTargetException e) {
            String msg = "Exception thrown while instantiating an instance of " + type;
            throw new IllegalArgumentException(msg, e);
        }
        T s = newInstance(type);
        if (init != PLACEHOLDER_MEMORY) {
            s.useMemory(init);
        }
        return s;
    }

    /**
     * Create a new Structure instance of the given type

View on GitHub (pinned to d036ad9781)

Solutions

  1. Use a concrete (non-abstract) Structure subclass; check that `type` is neither abstract nor an interface.
  2. Implement the abstract class as an anonymous or concrete subclass where it is used.
  3. Fix the underlying definition: if the abstract class is the intended field type, declare the field with the concrete implementation instead.
  4. Catch IllegalArgumentException and inspect the cause (InstantiationException) to identify which class is abstract.

Example fix

// before
public abstract class Msg extends Structure { ... }
Msg m = Structure.newInstance(Msg.class, ptr);
// after
public class MsgImpl extends Msg { ... }
Msg m = Structure.newInstance(MsgImpl.class, ptr);
Defensive patterns

Strategy: type-guard

Validate before calling

static <T extends Structure> void requireConcrete(Class<T> type) {
    int mods = type.getModifiers();
    if (java.lang.reflect.Modifier.isAbstract(mods) || type.isInterface())
        throw new IllegalStateException(type + " is abstract/interface; pass a concrete Structure subclass");
}

Type guard

static boolean isInstantiableStructure(Class<?> c) {
    int m = c.getModifiers();
    return Structure.class.isAssignableFrom(c)
        && !java.lang.reflect.Modifier.isAbstract(m)
        && !c.isInterface()
        && !java.lang.reflect.Modifier.isPrivate(m);
}

Try / catch

try {
    MyStruct s = Structure.newInstance(type, pointer);
} catch (IllegalArgumentException e) {
    if (e.getCause() instanceof InstantiationException) {
        throw new IllegalStateException(type + " is abstract; use a concrete subclass", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/2b9fbbdd60029129. Report an issue: GitHub.