{"record":{"id":"88e32c99bbb5221e","repo":"java-native-access/jna","slug":"arrays-of-length-zero-not-allowed-type","errorCode":null,"errorMessage":"Arrays of length zero not allowed: type","messagePattern":"Arrays of length zero not allowed: type","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Native.java","lineNumber":1465,"sourceCode":"            }\n        }\n    }\n\n    /**\n     * @param type The Java class for which the native size is to be determined\n     * @param value an instance of said class (if available)\n     * @return the native size of the given class, in bytes.\n     * For use with arrays.\n     */\n    public static int getNativeSize(Class<?> type, Object value) {\n        if (type.isArray()) {\n            int len = Array.getLength(value);\n            if (len > 0) {\n                Object o = Array.get(value, 0);\n                return len * getNativeSize(type.getComponentType(), o);\n            }\n            // Don't process zero-length arrays\n            throw new IllegalArgumentException(\"Arrays of length zero not allowed: \" + type);\n        }\n        if (Structure.class.isAssignableFrom(type)\n            && !Structure.ByReference.class.isAssignableFrom(type)) {\n            return Structure.size((Class<Structure>) type, (Structure)value);\n        }\n        try {\n            return getNativeSize(type);\n        }\n        catch(IllegalArgumentException e) {\n            throw new IllegalArgumentException(\"The type \\\"\" + type.getName()\n                                               + \"\\\" is not supported: \"\n                                               + e.getMessage());\n        }\n    }\n\n    /**\n     * Returns the native size for a given Java class.  Structures are\n     * assumed to be <code>struct</code> pointers unless they implement","sourceCodeStart":1447,"sourceCodeEnd":1483,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L1447-L1483","documentation":"Native.getNativeSize(type, value) computes the native byte size of an argument; for array types it multiplies the component size by the array length. A zero-length array has no meaningful native footprint and cannot be passed to native code, so JNA deliberately rejects it with this IllegalArgumentException.","triggerScenarios":"Calling a mapped native function (or Native.getNativeSize) with an empty Java array (e.g. new int[0], new byte[0]) as a parameter, or computing sizes for structure fields holding empty arrays.","commonSituations":"Passing result collections that happened to be empty straight to a native call, forgetting to guard varargs/buffer arguments converted from lists, or default-initialized arrays used as placeholder arguments.","solutions":["Guard the call site: skip the native invocation or pass null/NULL when the array is empty.","Allocate at least one element if the native API requires a non-empty buffer, or use a properly sized byte[]/Memory for 'no data'.","If the native side accepts a NULL pointer for empty input, declare the parameter as a pointer type and pass null for empty arrays.","For size queries, check array length > 0 before calling Native.getNativeSize."],"exampleFix":"// before\nlib.process(values); // values is int[0]\n\n// after\nif (values.length > 0) {\n    lib.process(values);\n} // or pass null / Pointer.NULL when the native API allows it","handlingStrategy":"validation","validationCode":"if (array == null || java.lang.reflect.Array.getLength(array) == 0) {\n    throw new IllegalArgumentException(\"Refusing to call native function with empty array\");\n}","typeGuard":"static boolean isNonEmptyArray(Object a) {\n    return a != null && a.getClass().isArray() && java.lang.reflect.Array.getLength(a) > 0;\n}","tryCatchPattern":"try {\n    lib.process(data);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().startsWith(\"Arrays of length zero\")) {\n        lib.process(Pointer.NULL); // if the native API accepts NULL\n    } else { throw e; }\n}","preventionTips":["Guard every mapped call site that receives collections converted to arrays.","Decide the empty-case protocol (skip call, pass null, or sentinel buffer) per native API and enforce it in wrapper methods.","Add unit tests covering empty-input behavior for each mapped function.","Prefer Pointer parameters with explicit length arguments for C APIs that allow NULL/zero-length input."],"tags":["jna","array","native-size","argument-validation"],"backgroundTag":"empty-required-field","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"}