java-native-access/jna · error · IllegalArgumentException

must be set from short/short[]

Error message

 must be set from short/short[]

What it means

For EvtVarTypeInt16 and EvtVarTypeUInt16, setValue requires a short (primitive, checked via getClass() == short.class) or a short[] for arrays. Any other type raises IllegalArgumentException '<TypeName> must be set from short/short[]'.

Source

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

                        } else {
                            throw new IllegalArgumentException(type.name() + " must be set from byte/byte[]");
                        }
                        break;
                    case EvtVarTypeInt16:
                    case EvtVarTypeUInt16:
                        if (value.getClass().isArray() && value.getClass().getComponentType() == short.class) {
                            Type = type.ordinal() | EVT_VARIANT_TYPE_ARRAY;
                            Memory mem = new Memory(((short[]) value).length * 2);
                            mem.write(0, (short[]) value, 0, ((short[]) value).length);
                            holder = mem;
                            Count = 0;
                            field1.writeField("pointerValue", mem);
                        } else if (value.getClass() == short.class) {
                            Type = type.ordinal();
                            Count = 0;
                            field1.writeField("shortValue", value);
                        } else {
                            throw new IllegalArgumentException(type.name() + " must be set from short/short[]");
                        }
                        break;
                    case EvtVarTypeHexInt32:
                    case EvtVarTypeInt32:
                    case EvtVarTypeUInt32:
                        if (value.getClass().isArray() && value.getClass().getComponentType() == int.class) {
                            Type = type.ordinal() | EVT_VARIANT_TYPE_ARRAY;
                            Memory mem = new Memory(((int[]) value).length * 4);
                            mem.write(0, (int[]) value, 0, ((int[]) value).length);
                            holder = mem;
                            Count = 0;
                            field1.writeField("pointerValue", mem);
                        } else if (value.getClass() == int.class) {
                            Type = type.ordinal();
                            Count = 0;
                            field1.writeField("intValue", value);
                        } else {
                            throw new IllegalArgumentException(type.name() + " must be set from int/int[]");

View on GitHub (pinned to d036ad9781)

Solutions

  1. Cast explicitly to short: (short) intValue, then pass it (a literal short survives as short.class)
  2. Convert arrays: loop or copy into short[] with per-element (short) casts
  3. For Long values, check range (0..65535 for unsigned) before casting to short

Example fix

// before
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeInt16, intValue);
// after
variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeInt16, (short) intValue);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value instanceof Integer) value = (short) ((Integer) value).intValue();
else if (value instanceof Long) value = (short) ((Long) value).longValue();

Type guard

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

Try / catch

try {
    variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeInt16, value);
} catch (IllegalArgumentException e) {
    variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeInt16,
        (short) ((Number) value).shortValue());
}

Prevention

When it happens

Trigger: setValue(EVT_VARIANT_TYPE.EvtVarTypeInt16, (Object) Integer.valueOf(5)) — an int/Integer passed where a 16-bit value is expected, since auto-widening does not happen because the parameter is Object.

Common situations: Parsing config values with Integer.parseInt and passing the int directly; passing Short wrapper object boxed from a collection; reading a value from JSON where numbers become Integer/Long.

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