{"record":{"id":"2ae38a81c29a6215","repo":"junit-team/junit5","slug":"cannot-convert-null-to-primitive-value-of-type-s","errorCode":null,"errorMessage":"Cannot convert null to primitive value of type %s","messagePattern":"Cannot convert null to primitive value of type (.+?)","errorType":"exception","errorClass":"ArgumentConversionException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/DefaultArgumentConverter.java","lineNumber":78,"sourceCode":"\tpublic final @Nullable Object convert(@Nullable Object source, ParameterContext context) {\n\t\tClass<?> targetType = context.getParameter().getType();\n\t\tClassLoader classLoader = getClassLoader(context.getDeclaringExecutable().getDeclaringClass());\n\t\treturn convert(source, targetType, classLoader);\n\t}\n\n\t@Override\n\tpublic final @Nullable Object convert(@Nullable Object source, FieldContext context)\n\t\t\tthrows ArgumentConversionException {\n\n\t\tClass<?> targetType = context.getField().getType();\n\t\tClassLoader classLoader = getClassLoader(context.getField().getDeclaringClass());\n\t\treturn convert(source, targetType, classLoader);\n\t}\n\n\tpublic final @Nullable Object convert(@Nullable Object source, Class<?> targetType, ClassLoader classLoader) {\n\t\tif (source == null) {\n\t\t\tif (targetType.isPrimitive()) {\n\t\t\t\tthrow new ArgumentConversionException(\n\t\t\t\t\t\"Cannot convert null to primitive value of type \" + targetType.getTypeName());\n\t\t\t}\n\t\t\treturn null;\n\t\t}\n\n\t\tif (ReflectionUtils.isAssignableTo(source, targetType)) {\n\t\t\treturn source;\n\t\t}\n\n\t\tif (source instanceof String string) {\n\t\t\ttry {\n\t\t\t\treturn convert(string, targetType, classLoader);\n\t\t\t}\n\t\t\tcatch (ConversionException ex) {\n\t\t\t\tthrow new ArgumentConversionException(ex.getMessage(), ex);\n\t\t\t}\n\t\t}\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/DefaultArgumentConverter.java#L60-L96","documentation":"Thrown by DefaultArgumentConverter.convert() when the source object is null and the target type is a primitive (int, long, boolean, byte, short, double, float, char). Primitives in Java cannot hold null values — null can only be assigned to reference types (including wrapper types like Integer). The error message includes the primitive type name.","triggerScenarios":"A @ParameterizedTest parameter or @Parameter field is a primitive type (e.g., int, boolean) and the argument source provides null for that position. This happens with CSV sources that have empty fields (which may parse as null), @NullSource, or custom ArgumentsProvider returning null values. The converter receives null and sees targetType.isPrimitive() == true.","commonSituations":"CSV parameterized tests where a column is sometimes empty, and the parameter is a primitive int. Using @NullSource alongside @ValueSource for a primitive parameter. Custom ArgumentsProvider that conditionally returns null for some invocations. JSON/YAML-based argument sources where a field is omitted and deserializes to null.","solutions":["Change the parameter type from primitive to its wrapper type (int → Integer, boolean → Boolean) to allow null","Ensure the argument source never provides null for primitive parameters — replace nulls with default values (0, false, etc.)","Remove @NullSource from tests with primitive parameters, or move null tests to a separate test with a wrapper-type parameter","For CSV sources, configure emptyValue handling or use @CsvFileSource with explicit null handling"],"exampleFix":"// before — primitive parameter with possible null source\n@ParameterizedTest\n@NullSource\n@ValueSource(ints = {1, 2, 3})\nvoid test(int value) { } // null → Cannot convert null to primitive\n\n// after — use wrapper type to allow null\n@ParameterizedTest\n@NullSource\n@ValueSource(ints = {1, 2, 3})\nvoid test(Integer value) {\n    if (value == null) return; // handle null explicitly\n    // ... test logic\n}","handlingStrategy":"validation","validationCode":"// Validate parameter types against possible null arguments before running\nimport java.lang.reflect.Parameter;\n\nstatic void validatePrimitiveParamsForNull(Method testMethod) {\n    for (Parameter p : testMethod.getParameters()) {\n        if (p.getType().isPrimitive()) {\n            System.out.println(\"WARNING: Parameter \" + p.getName()\n                + \" is primitive (\" + p.getType() + \").\"\n                + \" If the source may provide null, use the wrapper type instead.\");\n        }\n    }\n}","typeGuard":"// Type guard: check if a parameter type can accept null\nstatic boolean canAcceptNull(Class<?> type) {\n    return !type.isPrimitive(); // primitives cannot be null\n}\n\n// Usage: if (!canAcceptNull(paramType) && mayProduceNull(source)) { /* fix needed */ }","tryCatchPattern":null,"preventionTips":["Use wrapper types (Integer, Boolean, etc.) instead of primitives for parameterized test parameters that may receive null","Never combine @NullSource with primitive parameters — split null cases into a separate test with wrapper types","For CSV sources, check for empty columns and either provide defaults or use wrapper parameter types"],"tags":["converter","null","primitive-type","type-mismatch","junit-params"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}