java-native-access/jna · error · IllegalArgumentException

Invalid Structure field in " + getClass() + ", field name '"

Error message

Invalid Structure field in " + getClass() + ", field name '" + structField.name + "' (" + structField.type + "): " + e.getMessage()

What it means

While computing the layout with force=true, a field's native size lookup failed (IllegalArgumentException from getNativeSize). If a TypeMapper is configured (or force is set) JNA cannot silently defer, so it rethrows as IllegalArgumentException with the Structure class, field name and type. It means a field type still has no valid native mapping.

Source

Thrown at src/com/sun/jna/Structure.java:1437

                    throw new IllegalArgumentException(msg);
                }
            }

            if (value == null) {
                value = initializeField(structField.field, type);
            }

            try {
                structField.size = getNativeSize(nativeType, value);
                fieldAlignment = getNativeAlignment(nativeType, value, firstField);
            }
            catch(IllegalArgumentException e) {
                // Might simply not yet have a type mapper set yet
                if (!force && typeMapper == null) {
                    return null;
                }
                String msg = "Invalid Structure field in " + getClass() + ", field name '" + structField.name + "' (" + structField.type + "): " + e.getMessage();
                throw new IllegalArgumentException(msg, e);
            }

            // Align fields as appropriate
            if (fieldAlignment == 0) {
                throw new Error("Field alignment is zero for field '" + structField.name + "' within " + getClass());
            }
            info.alignment = Math.max(info.alignment, fieldAlignment);
            if ((calculatedSize % fieldAlignment) != 0) {
                calculatedSize += fieldAlignment - (calculatedSize % fieldAlignment);
            }
            if (this instanceof Union) {
                structField.offset = 0;
                calculatedSize = Math.max(calculatedSize, structField.size);
            }
            else {
                structField.offset = calculatedSize;
                calculatedSize += structField.size;
            }

View on GitHub (pinned to d036ad9781)

Solutions

  1. Fix the underlying field type mapping (see the cause message's type) with a supported type or a TypeMapper/NativeMapped
  2. Ensure the TypeMapper handles every field type on both to/from sides
  3. Avoid forcing size calculation until all fields are properly initialized and mapped
  4. Inspect the cause chain (msg, e) to identify the exact failing type

Example fix

// before
mapper lacks converter for CustomType; field: public CustomType x;
// after
// add to mapper:
getToNativeConverter(CustomType.class) / getFromNativeConverter(CustomType.class)
// or make CustomType implement NativeMapped
Defensive patterns

Strategy: validation

Validate before calling

for (Field f : S.class.getFields()) {
    try {
        // ensure each field type is sizeable before forcing layout
        if (typeMapper != null && typeMapper.getToNativeConverter(f.getType()) == null
            && !NativeMapped.class.isAssignableFrom(f.getType())) {
            throw new IllegalStateException("No size mapping for " + f);
        }
    } catch (IllegalArgumentException e) { throw new IllegalStateException(f + " unmapped", e); }
}

Try / catch

try {
    s.size();
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().contains("Invalid Structure field")) {
        // inspect e.getCause() to find the unmapped type and fix the mapper
        throw new IllegalStateException("Fix field mapping: " + e.getMessage(), e.getCause());
    }
    throw e;
}

Prevention

When it happens

Trigger: calculateSize(true)/size(true) or write paths where a field type has no native size even after type mapper consideration; typeMapper != null but the mapper does not handle the offending type; validation triggered with force on an incompletely-defined structure.

Common situations: TypeMapper registered but missing converters for some field types; nested structures that fail their own validation; using force during tests to surface layout problems early.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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