java-native-access/jna · error · java.lang.IllegalArgumentException
No field of type in
Error message
No field of type in
What it means
Union.setType(Class) throws IllegalArgumentException when the union's registered fields contain none whose Java type matches the requested type. JNA unions require the active field to be selected by exact field type before reading or writing native memory; this guard prevents silently reading garbage from an unmatched type.
Source
Thrown at src/com/sun/jna/Union.java:92
return list;
}
/** Indicates by type which field will be used to write to native memory.
* If there are multiple fields of the same type, use {@link
* #setType(String)} instead with the field name.
* @param type desired active type for the union
* @throws IllegalArgumentException if the type does not correspond to
* any declared union field.
*/
public void setType(Class<?> type) {
ensureAllocated();
for (StructField f : fields().values()) {
if (f.type == type) {
activeField = f;
return;
}
}
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);
}View on GitHub (pinned to d036ad9781)
Solutions
- Pass the exact declared type of one of the union's Java fields to setType (e.g. union.setType(MyStruct.class), not the interface it implements).
- Inspect the union subclass fields and pick the matching one, or use setType(String fieldName) with the field name instead.
- If the intended field type changed, update all call sites after refactoring, or use setTypedValue which finds the field by the value's runtime class.
Example fix
// before union.setType(Pointer.class); // no Pointer-typed field exists // after union.setType(MyStructure.class); // matches declared field type
Defensive patterns
Strategy: validation
Validate before calling
// Java
boolean hasFieldType(Union u, Class<?> type) {
for (java.lang.reflect.Field f : u.getClass().getFields()) {
if (!java.lang.reflect.Modifier.isStatic(f.getModifiers()) && f.getType() == type) return true;
}
return false;
}
if (!hasFieldType(union, MyStruct.class)) throw new IllegalStateException("union lacks " + MyStruct.class); Try / catch
try { union.setType(MyStruct.class); } catch (IllegalArgumentException e) { /* fall back to another field or fail fast */ } Prevention
- Always pass the exact declared field class, never superclasses or interfaces
- Prefer setType(String fieldName) so typos fail at compile-checked string sites less often
- Grep for setType calls when refactoring union field types
When it happens
Trigger: Calling union.setType(SomeClass.class) where SomeClass is not the declared type of any field added via the public no-arg fields of the Union subclass, or after the field type changed (e.g. Pointer vs. structure type) so it no longer matches exactly.
Common situations: Developers refactor a union field from a Structure to a Pointer (or vice versa) and forget to update setType calls; using a superclass/interface instead of the exact field type; mapping a union where the same native member was mapped with a wrapper type.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Insufficient memory to align to the requested boundary
- No field named in
- The given priority value is invalid!
- must be set from byte[]
- Unrecognized calling convention: <convention>
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/359bd5fb317a2a7e.
Report an issue: GitHub.