{"record":{"id":"e331062c7d16f729","repo":"java-native-access/jna","slug":"need-an-initialized-array","errorCode":null,"errorMessage":"Need an initialized array","messagePattern":"Need an initialized array","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Pointer.java","lineNumber":448,"sourceCode":"                result = currentValue;\n            }\n        } else if (NativeMapped.class.isAssignableFrom(type)) {\n            NativeMapped nm = (NativeMapped)currentValue;\n            if (nm != null) {\n                Object value = getValue(offset, nm.nativeType(), null);\n                result = nm.fromNative(value, new FromNativeContext(type));\n                if (nm.equals(result)) {\n                    result = nm;\n                }\n            } else {\n                NativeMappedConverter tc = NativeMappedConverter.getInstance(type);\n                Object value = getValue(offset, tc.nativeType(), null);\n                result = tc.fromNative(value, new FromNativeContext(type));\n            }\n        } else if (type.isArray()) {\n            result = currentValue;\n            if (result == null) {\n                throw new IllegalStateException(\"Need an initialized array\");\n            }\n            readArray(offset, result, type.getComponentType());\n        } else {\n            throw new IllegalArgumentException(\"Reading \\\"\" + type + \"\\\" from memory is not supported\");\n        }\n        return result;\n    }\n\n    /** Read memory starting at offset into the array with element type cls. */\n    private void readArray(long offset, Object o, Class<?> cls) {\n        int length = 0;\n        length = Array.getLength(o);\n        Object result = o;\n\n        if (cls == byte.class) {\n            read(offset, (byte[])result, 0, length);\n        }\n        else if (cls == short.class) {","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Pointer.java#L430-L466","documentation":"Pointer.getValue throws IllegalStateException when the target type is an array but the current value is null. JNA reads array data into the existing Java array object; it cannot allocate a new one during a memory read, so an initialized array must be supplied.","triggerScenarios":"Reading a Structure field typed as an array (e.g. byte[] or int[]) that was left null; calling Pointer.getValue(offset, int[].class, null) directly.","commonSituations":"Structures where the array field was never assigned before reading from native memory; partially mapped structs where only some fields are initialized.","solutions":["Initialize array fields before reading: new byte[len] with the correct length","Call Structure.read() only after all array fields are allocated","Use Structure array-field constructors or allocate in the field initializer","If length is dynamic, use Pointer + read(offset, array, 0, len) manually instead of typed array fields"],"exampleFix":"// before\nclass S extends Structure {\n    public byte[] data; // never initialized\n}\n// after\nclass S extends Structure {\n    public byte[] data = new byte[64];\n}","handlingStrategy":"validation","validationCode":"if (arr == null) {\n    arr = new byte[expectedLength];\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize all array fields in Structure definitions with fixed sizes","Call Structure.allocateMemory/read only after arrays are assigned","For dynamic sizes, read via Pointer.read(offset, array, 0, len) manually"],"tags":["jna","pointer","array","structure","uninitialized"],"backgroundTag":"null-argument","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}