{"record":{"id":"ce93099edfe38c54","repo":"java-native-access/jna","slug":"must-be-set-from-string-string","errorCode":null,"errorMessage":" must be set from String/String[]","messagePattern":" must be set from String/String\\[\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Winevt.java","lineNumber":298,"sourceCode":"                field1.writeField(\"pointerValue\", Pointer.NULL);\n            } else {\n                switch (type) {\n                    case EvtVarTypeAnsiString:\n                        if (value.getClass().isArray() && value.getClass().getComponentType() == String.class) {\n                            Type = type.ordinal() | EVT_VARIANT_TYPE_ARRAY;\n                            StringArray sa = new StringArray((String[]) value, false);\n                            holder = sa;\n                            Count = ((String[]) value).length;\n                            field1.writeField(\"pointerValue\", sa);\n                        } else if (value.getClass() == String.class) {\n                            Type = type.ordinal();\n                            Memory mem = new Memory(((String) value).length() + 1);\n                            mem.setString(0, (String) value);\n                            holder = mem;\n                            Count = 0;\n                            field1.writeField(\"pointerValue\", mem);\n                        } else {\n                            throw new IllegalArgumentException(type.name() + \" must be set from String/String[]\");\n                        }\n                        break;\n                    case EvtVarTypeBoolean:\n                        if (value.getClass().isArray() && value.getClass().getComponentType() == BOOL.class) {\n                            Type = type.ordinal() | EVT_VARIANT_TYPE_ARRAY;\n                            Memory mem = new Memory(((BOOL[]) value).length * 4);\n                            for (int i = 0; i < ((BOOL[]) value).length; i++) {\n                                mem.setInt(i * 4, ((BOOL[]) value)[i].intValue());\n                            }\n                            holder = mem;\n                            Count = 0;\n                            field1.writeField(\"pointerValue\", mem);\n                        } else if (value.getClass() == BOOL.class) {\n                            Type = type.ordinal();\n                            Count = 0;\n                            field1.writeField(\"intValue\", ((BOOL) value).intValue());\n                        } else {\n                            throw new IllegalArgumentException(type.name() + \" must be set from BOOL/BOOL[]\");","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Winevt.java#L280-L316","documentation":"For EvtVarTypeAnsiString (and similar single-byte string types), setValue requires the value to be a String (or a String[] for the array form). Any other object type is rejected with this IllegalArgumentException, prefixed with the variant type name (e.g. 'EvtVarTypeAnsiString must be set from String/String[]'). The message's leading space comes from concatenating type.name() with the literal.","triggerScenarios":"setValue(EVT_VARIANT_TYPE.EvtVarTypeAnsiString, value) where value is not a String and not a String[] — e.g. a byte[], a WideChar-backed object, Integer, or a CharSequence.","commonSituations":"Passing a value read from another variant/union without converting to String; assuming ANSI and wide string types are interchangeable; passing a byte[] that actually holds ASCII text.","solutions":["Convert the value to String: for byte[] use new String(bytes, StandardCharsets.US_ASCII) or StandardCharsets.UTF_8","Use EvtVarTypeString with a wide-string-compatible call if the data is actually wide (UTF-16)","Check the actual runtime class of value (value.getClass()) before the call to confirm it is String or String[]"],"exampleFix":"// before\nvariant.setValue(EVT_VARIANT_TYPE.EvtVarTypeAnsiString, asciiBytes);\n// after\nvariant.setValue(EVT_VARIANT_TYPE.EvtVarTypeAnsiString, new String(asciiBytes, StandardCharsets.US_ASCII));","handlingStrategy":"type-guard","validationCode":"if (!(value instanceof String) && !(value instanceof String[])) {\n    throw new IllegalArgumentException(\"AnsiString requires String or String[]\");\n}","typeGuard":"boolean isAnsiStringCompatible(Object v) {\n    return v instanceof String || (v != null && v.getClass() == String[].class);\n}","tryCatchPattern":"try {\n    variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeAnsiString, value);\n} catch (IllegalArgumentException e) {\n    value = String.valueOf(value); // fallback conversion, then retry\n    variant.setValue(EVT_VARIANT_TYPE.EvtVarTypeAnsiString, value);\n}","preventionTips":["Convert CharSequence/byte[] to String before setting ANSI string variants","Remember CharSequence is not String — call toString()","Check getComponentType() == String.class for array inputs"],"tags":["windows-eventlog","jna","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}