{"record":{"id":"6d09fce6b555b7f8","repo":"java-native-access/jna","slug":"setvalue-must-not-be-called-with-type-set-to-null","errorCode":null,"errorMessage":"setValue must not be called with type set to NULL","messagePattern":"setValue must not be called with type set to NULL","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/Winevt.java","lineNumber":274,"sourceCode":"        public boolean isArray() {\n            return (Type & EVT_VARIANT_TYPE_ARRAY) == EVT_VARIANT_TYPE_ARRAY;\n        }\n\n        public EVT_VARIANT_TYPE getVariantType() {\n            return EVT_VARIANT_TYPE.values()[getBaseType()];\n        }\n\n        // Helper to store java object for set values\n        private Object holder;\n\n        /**\n         * @param type\n         * @param value\n         */\n        public void setValue(EVT_VARIANT_TYPE type, Object value) {\n            allocateMemory();\n            if (type == null) {\n                throw new IllegalArgumentException(\"setValue must not be called with type set to NULL\");\n            }\n            holder = null;\n            if (value == null || type == EVT_VARIANT_TYPE.EvtVarTypeNull) {\n                Type = EVT_VARIANT_TYPE.EvtVarTypeNull.ordinal();\n                Count = 0;\n                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);","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/Winevt.java#L256-L292","documentation":"EVT_VARIANT.setValue() in JNA's Winevt helper wraps an EVT_VARIANT union for the Windows Event Log API. It throws IllegalArgumentException when the caller passes a null EVT_VARIANT_TYPE, because without a type there is no way to know which union field to populate. The null check runs before any switch on the type, so this always fails fast.","triggerScenarios":"Calling setValue(null, someValue) — e.g. passing an uninitialized or lookup-failed EVT_VARIANT_TYPE variable, or a method returning null type to renderChannelConfig/event-rendering code like testModifyChannelConfig.","commonSituations":"Mapping an EvtVariant field whose type was read from a struct and turned out null; passing the result of a map lookup (get(typeKey)) that missed; refactoring where the type variable was accidentally not initialized.","solutions":["Pass a non-null EVT_VARIANT_TYPE, e.g. EVT_VARIANT_TYPE.EvtVarTypeNull if you intend to clear the variant","If the type comes from a variable or lookup, check for null before calling setValue and substitute EvtVarTypeNull","If clearing the value, call setValue(EVT_VARIANT_TYPE.EvtVarTypeNull, null) instead of passing a null type"],"exampleFix":"// before\nvariant.setValue(null, myValue);\n// after\nvariant.setValue(EVT_VARIANT_TYPE.EvtVarTypeString, myValue);\n// or to clear:\nvariant.setValue(EVT_VARIANT_TYPE.EvtVarTypeNull, null);","handlingStrategy":"validation","validationCode":"if (type == null) {\n    type = EVT_VARIANT_TYPE.EvtVarTypeNull; // or throw a domain-specific error early\n}\nvariant.setValue(type, value);","typeGuard":"boolean isValidType(EVT_VARIANT_TYPE t) { return t != null; }","tryCatchPattern":"try {\n    variant.setValue(type, value);\n} catch (IllegalArgumentException e) {\n    log.warn(\"Bad EVT_VARIANT type/value: \" + e.getMessage());\n}","preventionTips":["Never pass a possibly-null enum obtained from a lookup map into setValue","Default unknown types to EvtVarTypeNull rather than null","Unit-test channel-config rendering with all EVT_VARIANT_TYPE values"],"tags":["windows-eventlog","jna","null-argument"],"backgroundTag":"null-argument","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"}