java-native-access/jna · error · java.lang.IllegalArgumentException

Arrays of length zero not allowed: type

Error message

Arrays of length zero not allowed: type

What it means

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.

Source

Thrown at src/com/sun/jna/Native.java:1465

            }
        }
    }

    /**
     * @param type The Java class for which the native size is to be determined
     * @param value an instance of said class (if available)
     * @return the native size of the given class, in bytes.
     * For use with arrays.
     */
    public static int getNativeSize(Class<?> type, Object value) {
        if (type.isArray()) {
            int len = Array.getLength(value);
            if (len > 0) {
                Object o = Array.get(value, 0);
                return len * getNativeSize(type.getComponentType(), o);
            }
            // Don't process zero-length arrays
            throw new IllegalArgumentException("Arrays of length zero not allowed: " + type);
        }
        if (Structure.class.isAssignableFrom(type)
            && !Structure.ByReference.class.isAssignableFrom(type)) {
            return Structure.size((Class<Structure>) type, (Structure)value);
        }
        try {
            return getNativeSize(type);
        }
        catch(IllegalArgumentException e) {
            throw new IllegalArgumentException("The type \"" + type.getName()
                                               + "\" is not supported: "
                                               + e.getMessage());
        }
    }

    /**
     * Returns the native size for a given Java class.  Structures are
     * assumed to be <code>struct</code> pointers unless they implement

View on GitHub (pinned to d036ad9781)

Solutions

  1. Guard the call site: skip the native invocation or pass null/NULL when the array is empty.
  2. Allocate at least one element if the native API requires a non-empty buffer, or use a properly sized byte[]/Memory for 'no data'.
  3. If the native side accepts a NULL pointer for empty input, declare the parameter as a pointer type and pass null for empty arrays.
  4. For size queries, check array length > 0 before calling Native.getNativeSize.

Example fix

// before
lib.process(values); // values is int[0]

// after
if (values.length > 0) {
    lib.process(values);
} // or pass null / Pointer.NULL when the native API allows it
Defensive patterns

Strategy: validation

Validate before calling

if (array == null || java.lang.reflect.Array.getLength(array) == 0) {
    throw new IllegalArgumentException("Refusing to call native function with empty array");
}

Type guard

static boolean isNonEmptyArray(Object a) {
    return a != null && a.getClass().isArray() && java.lang.reflect.Array.getLength(a) > 0;
}

Try / catch

try {
    lib.process(data);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Arrays of length zero")) {
        lib.process(Pointer.NULL); // if the native API accepts NULL
    } else { throw e; }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/88e32c99bbb5221e. Report an issue: GitHub.