java-native-access/jna · error · IllegalArgumentException

must be set from float/float[]

Error message

 must be set from float/float[]

What it means

For EvtVarTypeSingle (32-bit float), setValue requires a primitive float (getClass() == float.class) or a float[] array. Other types raise IllegalArgumentException '<TypeName> must be set from float/float[]'.

Solutions

  1. Cast to float: (float) doubleValue (accepting precision loss)
  2. Convert double[] to float[] element-wise in a loop or with Float.parseFloat(String.valueOf(d)) for exactness control
  3. Ensure boxed Float values are unboxed: floatValue.floatValue() — but note a single primitive float must be passed, wrap in float[] if needed

Example fix

// before
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeSingle, doubleValue);
// after
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeSingle, (float) doubleValue);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value instanceof Double) value = (float) ((Double) value).doubleValue();
else if (!(value instanceof Float)) value = (float) ((Number) value).doubleValue();

Type guard

boolean isFloatVariant(Object v) {
    return v != null && (v.getClass() == float.class || v.getClass() == float[].class);
}

Try / catch

try {
    variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeSingle, value);
} catch (IllegalArgumentException e) {
    variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeSingle, (float) ((Number) value).doubleValue());
}

Prevention

When it happens

Trigger: setValue(EVT_VARIANT_TYPE.EvtVarTypeSingle, doubleValue) — a double passed for a float field; or a Float/Double wrapper object, since getClass() must be exactly float.class.

Common situations: Measurements parsed with Double.parseDouble; values from JSON deserialized as Double; math expressions that default to double precision.

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/00ffe56c4a65a9f2. Report an issue: GitHub.

Appendix: source

Thrown at contrib/platform/src/com/sun/jna/platform/win32/Winevt.java:421

                            field1.writeField("longValue", value);
                        } else {
                            throw new IllegalArgumentException(type.name() + " must be set from long/long[]");
                        }
                        break;
                    case EvtVarTypeSingle:
                        if (value.getClass().isArray() && value.getClass().getComponentType() == float.class) {
                            Type = type.ordinal() | EVT_VARIANT_TYPE_ARRAY;
                            Memory mem = new Memory(((float[]) value).length * 4);
                            mem.write(0, (float[]) value, 0, ((float[]) value).length);
                            holder = mem;
                            Count = 0;
                            field1.writeField("pointerValue", mem);
                        } else if (value.getClass() == float.class) {
                            Type = type.ordinal();
                            Count = 0;
                            field1.writeField("floatValue", value);
                        } else {
                            throw new IllegalArgumentException(type.name() + " must be set from float/float[]");
                        }
                        break;
                    case EvtVarTypeDouble:
                        if (value.getClass().isArray() && value.getClass().getComponentType() == double.class) {
                            Type = type.ordinal() | EVT_VARIANT_TYPE_ARRAY;
                            Memory mem = new Memory(((double[]) value).length * 4);
                            mem.write(0, (double[]) value, 0, ((double[]) value).length);
                            holder = mem;
                            Count = 0;
                            field1.writeField("pointerValue", mem);
                        } else if (value.getClass() == double.class) {
                            Type = type.ordinal();
                            Count = 0;
                            field1.writeField("doubleVal", value);
                        } else {
                            throw new IllegalArgumentException(type.name() + " must be set from double/double[]");
                        }
                        break;

View on GitHub (pinned to d036ad9781)