java-native-access/jna · error · IllegalArgumentException

must be set from double/double[]

Error message

 must be set from double/double[]

What it means

For EvtVarTypeDouble, setValue requires a primitive double (getClass() == double.class, stored in doubleVal) or a double[] array. Other types raise IllegalArgumentException '<TypeName> must be set from double/double[]'.

Solutions

  1. Cast to double: (double) floatValue — widening is lossless
  2. Convert BigDecimal with bigDecimal.doubleValue(), Float with floatVal.doubleValue() (or pass primitive)
  3. Convert Float[]/List<Float> to double[] with element-wise casting

Example fix

// before
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeDouble, floatValue); // Float wrapper
// after
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeDouble, floatValue.doubleValue());
Defensive patterns

Strategy: type-guard

Validate before calling

if (value instanceof Float) value = (double) ((Float) value).floatValue();
else if (value instanceof BigDecimal) value = ((BigDecimal) value).doubleValue();

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: setValue(EVT_VARIANT_TYPE.EvtVarTypeDouble, floatValue) — a float passed for a double field; or a Float/BigDecimal wrapper whose class fails the exact getClass() == double.class check.

Common situations: Values from Float.parseFloat or float math; BigDecimal values from JSON/decimal parsing; boxed numbers from collections passed via Object.

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/42d96833be457020. Report an issue: GitHub.

Appendix: source

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

                            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;
                    case EvtVarTypeBinary:
                        if (value.getClass().isArray() && value.getClass().getComponentType() == byte.class) {
                            Type = type.ordinal();
                            Memory mem = new Memory(((byte[]) value).length * 1);
                            mem.write(0, (byte[]) value, 0, ((byte[]) value).length);
                            holder = mem;
                            Count = 0;
                            field1.writeField("pointerValue", mem);
                        } else {
                            throw new IllegalArgumentException(type.name() + " must be set from byte[]");
                        }
                        break;
                    case EvtVarTypeFileTime:
                    case EvtVarTypeEvtHandle:
                    case EvtVarTypeSysTime:
                    case EvtVarTypeGuid:

View on GitHub (pinned to d036ad9781)