{"record":{"id":"d19dcb44b436e9f8","repo":"java-native-access/jna","slug":"reading-array-of-cls-from-memory-not-supported","errorCode":null,"errorMessage":"Reading array of \" + cls + \" from memory not supported","messagePattern":"Reading array of \" \\+ cls \\+ \" from memory not supported","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Pointer.java","lineNumber":529,"sourceCode":"                    }\n                    else {\n                        sarray[i].useMemory(this, (int)(offset + i * sarray[i].size()), true);\n                        sarray[i].read();\n                    }\n                }\n            }\n        }\n        else if (NativeMapped.class.isAssignableFrom(cls)) {\n            NativeMapped[] array = (NativeMapped[])result;\n            NativeMappedConverter tc = NativeMappedConverter.getInstance(cls);\n            int size = Native.getNativeSize(result.getClass(), result) / array.length;\n            for (int i=0;i < array.length;i++) {\n                Object value = getValue(offset + size*i, tc.nativeType(), array[i]);\n                array[i] = (NativeMapped)tc.fromNative(value, new FromNativeContext(cls));\n            }\n        }\n        else {\n            throw new IllegalArgumentException(\"Reading array of \"\n                                               + cls\n                                               + \" from memory not supported\");\n        }\n    }\n\n    /**\n     * Indirect the native pointer as a pointer to <code>byte</code>.  This is\n     * equivalent to the expression\n     * <code>*((jbyte *)((char *)Pointer + offset))</code>.\n     *\n     * @param offset offset from pointer to perform the indirection\n     * @return the <code>byte</code> value being pointed to\n     */\n    public byte getByte(long offset) {\n        return Native.getByte(this, this.peer, offset);\n    }\n\n    /**","sourceCodeStart":511,"sourceCodeEnd":547,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Pointer.java#L511-L547","documentation":"Pointer.readArray (invoked from getValue for array types) encountered an array component type it cannot read from native memory. After handling byte/short/char/int/long/float/double/Pointer/Structure/NativeMapped arrays, any remaining element type is rejected with IllegalArgumentException.","triggerScenarios":"Calling pointer.getValue(offset, SomeArrayClass.class, arrayInstance) — or a Structure field read — where the array's component type is not a primitive, Pointer, Structure, or NativeMapped; e.g. an array of String[] elements or of arbitrary objects.","commonSituations":"Declaring a Structure field as String[] or Object[] expecting JNA to map it; reading arrays of custom POJOs that were never given a NativeMapped or Structure mapping; generics erasure leaving an unsupported component type.","solutions":["Use a supported array component type: primitive arrays, Pointer[], Structure[], or NativeMapped implementations.","Replace String[] structure fields with a single String (native char**) mapped via Pointer, or define a Structure describing the native layout.","For custom element types, make the component class implement NativeMapped or extend Structure.","Read the raw bytes/pointers yourself via pointer.getPointer/getByteArray and convert manually."],"exampleFix":"// before\nString[] arr = (String[]) pointer.getValue(0, String[].class, new String[4]);\n// after\nPointer[] ptrs = (Pointer[]) pointer.getValue(0, Pointer[].class, new Pointer[4]);","handlingStrategy":"validation","validationCode":"static void requireSupportedArrayComponent(Class<?> component) {\n    boolean ok = component.isPrimitive()\n        || Pointer.class.isAssignableFrom(component)\n        || Structure.class.isAssignableFrom(component)\n        || NativeMapped.class.isAssignableFrom(component);\n    if (!ok) throw new IllegalArgumentException(\"Unsupported array element type: \" + component);\n}","typeGuard":"static boolean isReadableArray(Class<?> arrayType) {\n    Class<?> c = arrayType.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.getValue(offset, array.getClass(), array);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Reading array of\")) {\n        // read element-wise via supported types instead\n    } else throw e;\n}","preventionTips":["Avoid String[], Object[], or interface-typed arrays in Structure definitions.","Model native pointer arrays as Pointer[] and dereference individually.","Make custom element types extend Structure or implement NativeMapped.","Check component type support before calling getValue with an array type."],"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"}