java-native-access/jna · error · IllegalArgumentException
Exception thrown while instantiating an instance of " + type
Error message
Exception thrown while instantiating an instance of " + type
What it means
When Structure.newInstance(Class, Pointer) invokes the structure's (Pointer) constructor and that constructor itself throws an exception, JNA wraps the InvocationTargetException as IllegalArgumentException with "Exception thrown while instantiating an instance of <type>". The real error is the cause inside the user's constructor, not JNA itself.
Source
Thrown at src/com/sun/jna/Structure.java:1960
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
* @param type desired Structure type
* @return the new instance
* @throws IllegalArgumentException if the instantiation fails
*/
public static <T extends Structure> T newInstance(Class<T> type) throws IllegalArgumentException {
T s = Klass.newInstance(type);
if (s instanceof ByValue) {
s.allocateMemory();View on GitHub (pinned to d036ad9781)
Solutions
- Inspect getCause() / the 'Caused by' of the IllegalArgumentException to find the exception thrown inside the constructor.
- Guard the (Pointer) constructor against placeholder instances used for size calculation (e.g. avoid read() or validation on null/zero pointers).
- Move expensive validation out of the constructor into a separate method invoked after JNA fully initializes the struct.
- Fix field initializers that can throw (null dereferences, array sizing, NativeMapped defaults) so construction succeeds.
Example fix
// before
public MyStruct(Pointer p) { super(p); if (p == null) throw new IllegalStateException("null"); read(); }
// after
public MyStruct(Pointer p) { super(p); if (p != null) read(); } Defensive patterns
Strategy: try-catch
Try / catch
try {
MyStruct s = Structure.newInstance(MyStruct.class, ptr);
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof InvocationTargetException) {
Throwable real = e.getCause().getCause();
throw new IllegalStateException("Constructor of MyStruct threw: " + real, real);
}
throw e;
} Prevention
- Never throw from Structure constructors - keep them trivial (super(p); read()) and validate later
- Avoid read()-time validation inside constructors: JNA creates placeholder instances during size calculation that may not satisfy assumptions
- Null-check the Pointer argument rather than throwing when JNA probes the class
- Always unwrap InvocationTargetException (via getCause) to see the actual constructor exception
When it happens
Trigger: A Structure's (Pointer) constructor (or super-constructor / instance initializers it runs, e.g. read(), field init, validate()) throws any RuntimeException or Error; triggered via Structure.newInstance(type, pointer), nested by-value field initialization, or JNA materializing a native return value.
Common situations: Constructor calls read() and a field-order validation fails; constructor dereferences a null Pointer; constructor has assertions or validation that fail on PLACEHOLDER_MEMORY instances JNA creates for size calculation; exceptions from field initializers of the subclass.
Related errors
- Instantiation of " + type + " (Pointer) not allowed, is it p
- No suitable constructor found for class:
- Can't create an instance of <klass>, requires a public no-ar
- This VM does not support read-only fields (field '" + field.
- Exception reading field '" + f.getName() + "' in " + getClas
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/ab845eb34983eae0.
Report an issue: GitHub.