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

No field named in

Error message

No field named  in 

What it means

Union.setType(String) throws IllegalArgumentException when the given field name does not correspond to any field registered on the union. JNA looks the name up in its field map and refuses to activate a nonexistent member, keeping the union's native layout consistent.

Source

Thrown at src/com/sun/jna/Union.java:108

            }
        }
        throw new IllegalArgumentException("No field of type " + type + " in " + this);
    }

    /**
     * Indicates which field will be used to write to native memory.
     * @param fieldName desired field to use for the active union type
     * @throws IllegalArgumentException if the name does not correspond to
     * any declared union field.
     */
    public void setType(String fieldName) {
        ensureAllocated();
        StructField f = fields().get(fieldName);
        if (f != null) {
            activeField = f;
        }
        else {
            throw new IllegalArgumentException("No field named " + fieldName
                                               + " in " + this);
        }
    }

    /** Force a read of the given field from native memory.
     * @return the new field value, after updating
     * @throws IllegalArgumentException if no field exists with the given name
     */
    @Override
    public Object readField(String fieldName) {
        ensureAllocated();
        setType(fieldName);
        return super.readField(fieldName);
    }

    /** Write the given field value to native memory.
     * The given field will become the active one.
     * @throws IllegalArgumentException if no field exists with the given name

View on GitHub (pinned to d036ad9781)

Solutions

  1. Correct the field name to exactly match the declared Java field in the Union subclass.
  2. Enumerate fields via the union's field map or use setType(Class) with the field's type instead.
  3. Use setTypedValue(value) to select the active field by the value's runtime type.

Example fix

// before
union.setType("intVal"); // field is actually named intValue
// after
union.setType("intValue");
Defensive patterns

Strategy: validation

Validate before calling

// Java
boolean hasFieldName(Union u, String name) {
    try { u.getClass().getField(name); return true; } catch (NoSuchFieldException e) { return false; }
}

Try / catch

try { union.setType("intValue"); } catch (IllegalArgumentException e) { log.error("unknown union field", e); }

Prevention

When it happens

Trigger: Calling union.setType("fieldName") with a misspelled name, a name of a field not declared in the Union subclass, or a name that changed during refactoring.

Common situations: Typo in field name; renaming a union field without updating setType callers; dynamically building the name from data that doesn't match declarations.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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