{"record":{"id":"3603212d335ff0c2","repo":"java-native-access/jna","slug":"structure-array-must-have-non-zero-length","errorCode":null,"errorMessage":"Structure array must have non-zero length","messagePattern":"Structure array must have non-zero length","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Function.java","lineNumber":615,"sourceCode":"                    for (int i=0;i < ss.length;i++) {\n                        if (ss[i] instanceof Structure.ByReference) {\n                            throw new IllegalArgumentException(\"Function \" + getName()\n                                                               + \" declared Structure[] at parameter \"\n                                                               + index + \" but element \" + i\n                                                               + \" is of Structure.ByReference type\");\n                        }\n                    }\n                }\n            }\n            if (byRef) {\n                Structure.autoWrite(ss);\n                Pointer[] pointers = new Pointer[ss.length + 1];\n                for (int i=0;i < ss.length;i++) {\n                    pointers[i] = ss[i] != null ? ss[i].getPointer() : null;\n                }\n                return new PointerArray(pointers);\n            } else if (ss.length == 0) {\n                throw new IllegalArgumentException(\"Structure array must have non-zero length\");\n            } else if (ss[0] == null) {\n                Structure.newInstance((Class<? extends Structure>) type).toArray(ss);\n                return ss[0].getPointer();\n            } else {\n                Structure.autoWrite(ss);\n                return ss[0].getPointer();\n            }\n        } else if (argClass.isArray()){\n            throw new IllegalArgumentException(\"Unsupported array argument type: \"\n                                               + argClass.getComponentType());\n        } else if (allowObjects) {\n            return arg;\n        } else if (!Native.isSupportedNativeType(arg.getClass())) {\n            throw new IllegalArgumentException(\"Unsupported argument type \"\n                                               + arg.getClass().getName()\n                                               + \" at parameter \" + index\n                                               + \" of function \" + getName());\n        }","sourceCodeStart":597,"sourceCodeEnd":633,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Function.java#L597-L633","documentation":"A zero-length Structure array cannot be marshaled because JNA derives the native pointer from the first element (ss[0].getPointer()); there is no element to take an address from. JNA therefore rejects empty arrays with IllegalArgumentException.","triggerScenarios":"Passing new MyStruct[0] (or a List.toArray into an empty array) as a Structure[] argument to a native function.","commonSituations":"Passing a Java-side empty collection result straight into a native call without a null-pointer / count protocol; native API expecting (NULL, 0) pattern that the Java mapping doesn't model.","solutions":["Guard the call site: if the array is empty, pass Pointer.NULL (or null) instead of an empty Structure[]","Model the native API as (Pointer, int count) and pass null plus 0 for the empty case","Allocate at least one element if the native API requires a real array (even a dummy)"],"exampleFix":"// before\nf(new MyStruct[0]);\n// after\nf(items.isEmpty() ? null : items.toArray(new MyStruct[0]));","handlingStrategy":"validation","validationCode":"if (ss.length == 0) {\n    nativeCall(null, 0); // or pass Pointer.NULL per your API mapping\n} else {\n    nativeCall(ss, ss.length);\n}","typeGuard":null,"tryCatchPattern":"try {\n    f.invoke(...);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"non-zero length\")) { /* pass NULL/0 instead */ }\n}","preventionTips":["Model native (ptr,count) pairs explicitly and pass null/0 for empty sets","Always check collection size at the boundary before converting to Structure[]"],"tags":["jna","empty-array","structure","illegal-argument"],"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-14T11:17:12.474Z"}