{"record":{"id":"1f0fe3eacc99a381","repo":"java-native-access/jna","slug":"writing-type-to-memory-is-not-supported","errorCode":null,"errorMessage":"Writing \" + type + \" to memory is not supported","messagePattern":"Writing \" \\+ type \\+ \" to memory is not supported","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Pointer.java","lineNumber":897,"sourceCode":"            }\n            else {\n                s.useMemory(this, (int)offset, true);\n                s.write();\n            }\n        } else if (Callback.class.isAssignableFrom(type)) {\n            setPointer(offset, CallbackReference.getFunctionPointer((Callback)value));\n        } else if (Platform.HAS_BUFFERS && Buffer.class.isAssignableFrom(type)) {\n            Pointer p = value == null ? null\n                : Native.getDirectBufferPointer((Buffer)value);\n            setPointer(offset, p);\n        } else if (NativeMapped.class.isAssignableFrom(type)) {\n            NativeMappedConverter tc = NativeMappedConverter.getInstance(type);\n            Class<?> nativeType = tc.nativeType();\n            setValue(offset, tc.toNative(value, new ToNativeContext()), nativeType);\n        } else if (type.isArray()) {\n            writeArray(offset, value, type.getComponentType());\n        } else {\n            throw new IllegalArgumentException(\"Writing \" + type + \" to memory is not supported\");\n        }\n    }\n\n    /** Write memory starting at offset from the array with element type cls. */\n    private void writeArray(long offset, Object value, Class<?> cls) {\n        if (cls == byte.class) {\n            byte[] buf = (byte[])value;\n            write(offset, buf, 0, buf.length);\n        } else if (cls == short.class) {\n            short[] buf = (short[])value;\n            write(offset, buf, 0, buf.length);\n        } else if (cls == char.class) {\n            char[] buf = (char[])value;\n            write(offset, buf, 0, buf.length);\n        } else if (cls == int.class) {\n            int[] buf = (int[])value;\n            write(offset, buf, 0, buf.length);\n        } else if (cls == long.class) {","sourceCodeStart":879,"sourceCodeEnd":915,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Pointer.java#L879-L915","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npointer.setValue(0, new int[]{1,2,3}, List.class);\n// after\nMemory mem = new Memory(8);\nmem.writeInt(0, 1); mem.writeInt(4, 2);","handlingStrategy":"type-guard","validationCode":"private static boolean isWritableType(Object v, Class<?> t) {\n    return (v instanceof Number) || (v instanceof Character) || (v instanceof Boolean)\n        || (v instanceof String) || (v instanceof WString) || (v instanceof Pointer)\n        || (v instanceof Structure) || (v instanceof Callback)\n        || (v instanceof NativeMapped) || t.isPrimitive() || t.isArray();\n}\n// call before setValue: if (!isWritableType(value, type)) convert first;","typeGuard":"static boolean isJnaWritable(Object v) {\n    return v instanceof Number || v instanceof Character || v instanceof Boolean\n        || v instanceof String || v instanceof Pointer || v instanceof Structure\n        || v instanceof Callback || v instanceof NativeMapped || v.getClass().isArray();\n}","tryCatchPattern":"try {\n    pointer.setValue(offset, value, type);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Writing\")) {\n        // convert to supported type / NativeMapped and retry\n    } else throw e;\n}","preventionTips":["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."],"tags":["java","jna","native-memory","type-mapping"],"backgroundTag":"unsupported-operation","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}