java-native-access/jna · error · IllegalArgumentException
must be set from long/long[]
Error message
must be set from long/long[]
What it means
For EvtVarTypeHexInt64, EvtVarTypeInt64 and EvtVarTypeUInt64, setValue requires a primitive long (getClass() == long.class) or a long[] array. Other types raise IllegalArgumentException '<TypeName> must be set from long/long[]'.
Source
Thrown at contrib/platform/src/com/sun/jna/platform/win32/Winevt.java:405
throw new IllegalArgumentException(type.name() + " must be set from int/int[]");
}
break;
case EvtVarTypeHexInt64:
case EvtVarTypeInt64:
case EvtVarTypeUInt64:
if (value.getClass().isArray() && value.getClass().getComponentType() == long.class) {
Type = type.ordinal() | EVT_VARIANT_TYPE_ARRAY;
Memory mem = new Memory(((long[]) value).length * 4);
mem.write(0, (long[]) value, 0, ((long[]) value).length);
holder = mem;
Count = 0;
field1.writeField("pointerValue", mem);
} else if (value.getClass() == long.class) {
Type = type.ordinal();
Count = 0;
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;View on GitHub (pinned to d036ad9781)
Solutions
- Cast to long: (long) intValue — widening is safe for signed values
- Convert List<Long>/Long[] to long[] with a loop or Arrays.stream(longBoxed).mapToLong(Long::longValue).toArray()
- For BigInteger use bigInteger.longValueExact() to avoid silent truncation
Example fix
// before variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeUInt64, integerList.toArray()); // Integer[] // after long[] longs = new long[integerList.size()]; for (int i = 0; i < longs.length; i++) longs[i] = integerList.get(i).longValue(); variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeUInt64, longs);
Defensive patterns
Strategy: type-guard
Validate before calling
if (value instanceof Integer) value = (long) (Integer) value; else if (value instanceof BigInteger) value = ((BigInteger) value).longValueExact();
Type guard
boolean isLongVariant(Object v) {
return v != null && (v.getClass() == long.class || v.getClass() == long[].class);
} Try / catch
try {
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeUInt64, value);
} catch (IllegalArgumentException e) {
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeUInt64, ((Number) value).longValue());
} Prevention
- Widen ints to long explicitly before setting 64-bit variants
- Use longValueExact() for BigInteger to avoid silent truncation
- Convert boxed Long collections to primitive long[] arrays
When it happens
Trigger: setValue(EVT_VARIANT_TYPE.EvtVarTypeUInt64, intValue) — an int passed for a 64-bit field; or an Integer/Long boxed in a collection so getClass() is the wrapper class.
Common situations: Timestamps or file times kept as Integer; values from List<Long> converted with toArray() giving Long[]; BigIntegers from parsing hex strings.
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
- must be set from String/String[]
- must be set from BOOL/BOOL[]
- must be set from byte/byte[]
- must be set from short/short[]
- must be set from int/int[]
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/19bdce3eed95a495.
Report an issue: GitHub.