{"record":{"id":"883436c6f408e3b2","repo":"java-native-access/jna","slug":"writing-array-of-cls-to-memory-not-supported","errorCode":null,"errorMessage":"Writing array of \" + cls + \" to memory not supported","messagePattern":"Writing array of \" \\+ cls \\+ \" to memory not supported","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Pointer.java","lineNumber":969,"sourceCode":"                    if (sbuf[i] == null) {\n                        sbuf[i] = tmp[i];\n                    } else {\n                        sbuf[i].useMemory(this, (int)(offset + i * sbuf[i].size()), true);\n                    }\n                    sbuf[i].write();\n                }\n            }\n        } else if (NativeMapped.class.isAssignableFrom(cls)) {\n            NativeMapped[] buf = (NativeMapped[])value;\n            NativeMappedConverter tc = NativeMappedConverter.getInstance(cls);\n            Class<?> nativeType = tc.nativeType();\n            int size = Native.getNativeSize(value.getClass(), value) / buf.length;\n            for (int i=0;i < buf.length;i++) {\n                Object element = tc.toNative(buf[i], new ToNativeContext());\n                setValue(offset + i*size, element, nativeType);\n            }\n        } else {\n            throw new IllegalArgumentException(\"Writing array of \"\n                                               + cls + \" to memory not supported\");\n        }\n    }\n\n    /** Write <code>value</code> to the requested bank of memory.\n     * @param offset byte offset from pointer to start\n     * @param length number of bytes to write\n     * @param value value to be written\n     */\n    public void setMemory(long offset, long length, byte value) {\n        Native.setMemory(this, this.peer, offset, length, value);\n    }\n\n    /**\n     * Set <code>value</code> at location being pointed to. This is equivalent\n     * to the expression\n     * <code>*((jbyte *)((char *)Pointer + offset)) = value</code>.\n     *","sourceCodeStart":951,"sourceCodeEnd":987,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Pointer.java#L951-L987","documentation":"Pointer.writeArray reached an array element type it cannot serialize into native memory. All supported component types (primitives, Pointer, Structure, NativeMapped) are handled earlier; anything else falls through to this IllegalArgumentException.","triggerScenarios":"Calling pointer.setValue(offset, arrayOfX, componentClass) — or writing a Structure containing such an array — where the component type is e.g. String[], Object[], or an unmapped custom class.","commonSituations":"Declaring String[] or interface-typed arrays in a Structure and calling structure.write(); attempting to persist arrays of arbitrary Java objects to a native buffer; generics leaving an erased/unsupported component type.","solutions":["Use arrays with supported component types: primitive arrays, Pointer[], Structure[], or NativeMapped-implementing classes.","For strings-as-pointer-arrays, allocate a Memory of pointers and write each String with pointer.setString manually.","Give the element class a Structure or NativeMapped mapping describing its native layout.","Write the buffer element-by-element with setValue per element using a supported type."],"exampleFix":"// before\npointer.setValue(0, new String[]{\"a\",\"b\"}, String.class);\n// after\nMemory arr = new Memory(2 * Native.POINTER_SIZE);\narr.setPointer(0, new NativeString(\"a\").getPointer());\narr.setPointer(Native.POINTER_SIZE, new NativeString(\"b\").getPointer());","handlingStrategy":"validation","validationCode":"static void requireWritableArray(Object arr) {\n    Class<?> c = arr.getClass().getComponentType();\n    boolean ok = c != null && (c.isPrimitive() || Pointer.class.isAssignableFrom(c)\n        || Structure.class.isAssignableFrom(c) || NativeMapped.class.isAssignableFrom(c));\n    if (!ok) throw new IllegalArgumentException(\"Cannot write array of \" + c);\n}","typeGuard":"static boolean isWritableArray(Object arr) {\n    Class<?> c = arr.getClass().getComponentType();\n    return c != null && (c.isPrimitive() || Pointer.class.isAssignableFrom(c)\n        || Structure.class.isAssignableFrom(c) || NativeMapped.class.isAssignableFrom(c));\n}","tryCatchPattern":"try {\n    pointer.setValue(offset, array, componentType);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Writing array of\")) {\n        // write elements individually with supported types\n    } else throw e;\n}","preventionTips":["Use primitive arrays, Pointer[], Structure[], or NativeMapped[] for native writes.","Write pointer-to-string arrays manually via Memory.setPointer + NativeString.","Map custom element classes as Structure or NativeMapped before array writes.","Validate component types in Structure field setters."],"tags":["java","jna","native-memory","arrays"],"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"}