java-native-access/jna · error · IllegalArgumentException
Writing " + type + " to memory is not supported
Error message
Writing " + type + " to memory is not supported
What it means
Pointer.setValue() was handed a Java value whose runtime type cannot be converted into native memory. JNA supports primitives, wrappers, String, Structure, Callback, Pointer and NativeMapped values; anything else (or any unrecognized type) throws IllegalArgumentException instead of writing corrupt data.
Source
Thrown at src/com/sun/jna/Pointer.java:897
}
else {
s.useMemory(this, (int)offset, true);
s.write();
}
} else if (Callback.class.isAssignableFrom(type)) {
setPointer(offset, CallbackReference.getFunctionPointer((Callback)value));
} else if (Platform.HAS_BUFFERS && Buffer.class.isAssignableFrom(type)) {
Pointer p = value == null ? null
: Native.getDirectBufferPointer((Buffer)value);
setPointer(offset, p);
} else if (NativeMapped.class.isAssignableFrom(type)) {
NativeMappedConverter tc = NativeMappedConverter.getInstance(type);
Class<?> nativeType = tc.nativeType();
setValue(offset, tc.toNative(value, new ToNativeContext()), nativeType);
} else if (type.isArray()) {
writeArray(offset, value, type.getComponentType());
} else {
throw new IllegalArgumentException("Writing " + type + " to memory is not supported");
}
}
/** Write memory starting at offset from the array with element type cls. */
private void writeArray(long offset, Object value, Class<?> cls) {
if (cls == byte.class) {
byte[] buf = (byte[])value;
write(offset, buf, 0, buf.length);
} else if (cls == short.class) {
short[] buf = (short[])value;
write(offset, buf, 0, buf.length);
} else if (cls == char.class) {
char[] buf = (char[])value;
write(offset, buf, 0, buf.length);
} else if (cls == int.class) {
int[] buf = (int[])value;
write(offset, buf, 0, buf.length);
} else if (cls == long.class) {View on GitHub (pinned to d036ad9781)
Solutions
- Convert the value to a supported type (primitive, wrapper, String, Pointer, Structure, Callback) before writing.
- If the value is a custom type, implement NativeMapped so JNA can convert it via toNative().
- For arrays, pass an array of a supported component type so writeArray() handles it.
- Check the exact type string in the message and fix the argument passed as `type` to setValue.
Example fix
// before
pointer.setValue(0, new int[]{1,2,3}, List.class);
// after
Memory mem = new Memory(8);
mem.writeInt(0, 1); mem.writeInt(4, 2); Defensive patterns
Strategy: type-guard
Validate before calling
private static boolean isWritableType(Object v, Class<?> t) {
return (v instanceof Number) || (v instanceof Character) || (v instanceof Boolean)
|| (v instanceof String) || (v instanceof WString) || (v instanceof Pointer)
|| (v instanceof Structure) || (v instanceof Callback)
|| (v instanceof NativeMapped) || t.isPrimitive() || t.isArray();
}
// call before setValue: if (!isWritableType(value, type)) convert first; Type guard
static boolean isJnaWritable(Object v) {
return v instanceof Number || v instanceof Character || v instanceof Boolean
|| v instanceof String || v instanceof Pointer || v instanceof Structure
|| v instanceof Callback || v instanceof NativeMapped || v.getClass().isArray();
} Try / catch
try {
pointer.setValue(offset, value, type);
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Writing")) {
// convert to supported type / NativeMapped and retry
} else throw e;
} Prevention
- Only pass primitives, wrappers, String, Pointer, Structure, Callback, or NativeMapped to setValue.
- Implement NativeMapped.toNative for custom domain objects written to memory.
- Ensure the `type` argument matches the value's actual runtime class.
- Model collections externally (allocate buffers) rather than writing them directly.
When it happens
Trigger: Calling pointer.setValue(offset, value, type) where `type`/value is an unsupported class — e.g. setValue(0, myList, List.class) or writing a field of a Structure whose declared type JNA cannot map.
Common situations: Setting a Structure field to a List/Map/custom POJO and calling write(); passing the wrong `type` argument (e.g. boxed/array mismatch) to setValue directly; library version where a newly used type lacks a converter.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Reading \"" + type + "\" from memory is not supported
- Reading array of " + cls + " from memory not supported
- Writing array of " + cls + " to memory not supported
- No support for " + os
- Set sun.java2d.noddraw=true to enable transparent windows
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/1f0fe3eacc99a381.
Report an issue: GitHub.