{"record":{"id":"773d075595e6acf0","repo":"junit-team/junit5","slug":"cannot-convert-null-to-primitive-value-of-type-s-773d07","errorCode":null,"errorMessage":"Cannot convert null to primitive value of type %s","messagePattern":"Cannot convert null to primitive value of type (.+?)","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java","lineNumber":110,"sourceCode":"\t *\n\t * @param source the source {@code String} to convert; may be {@code null}\n\t * but only if the target type is a reference type\n\t * @param targetType the target type the source should be converted into;\n\t * never {@code null}\n\t * @param classLoader the {@code ClassLoader} to use; may be {@code null} to\n\t * use the default {@code ClassLoader}\n\t * @param <T> the type of the target\n\t * @return the converted object; may be {@code null} but only if the target\n\t * type is a reference type\n\t *\n\t * @since 1.11\n\t */\n\t@SuppressWarnings(\"unchecked\")\n\tpublic static <T> @Nullable T convert(@Nullable String source, Class<T> targetType,\n\t\t\t@Nullable ClassLoader classLoader) {\n\t\tif (source == null) {\n\t\t\tif (targetType.isPrimitive()) {\n\t\t\t\tthrow new ConversionException(\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 (String.class.equals(targetType)) {\n\t\t\treturn (T) source;\n\t\t}\n\n\t\tClass<?> targetTypeToUse = toWrapperType(targetType);\n\t\tOptional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(\n\t\t\tcandidate -> candidate.canConvertTo(targetTypeToUse)).findFirst();\n\t\tif (converter.isPresent()) {\n\t\t\ttry {\n\t\t\t\tClassLoader classLoaderToUse = classLoader != null ? classLoader\n\t\t\t\t\t\t: ClassLoaderUtils.getDefaultClassLoader();\n\t\t\t\treturn (T) converter.get().convert(source, targetTypeToUse, classLoaderToUse);\n\t\t\t}","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java#L92-L128","documentation":"Thrown by ConversionSupport.convert when the source string is null but the target type is a primitive (int, boolean, char, ...). Java primitives cannot hold null, so the conversion is impossible and JUnit treats it as a hard contract violation rather than returning a default. The targetType.getTypeName() is interpolated into the message.","triggerScenarios":"Calling ConversionSupport.convert(null, int.class, loader) (or any primitive .class), or a parameterized-test argument source feeding null into a primitive parameter: @NullSource / @NullAndEmptySource on an int param, a @MethodSource returning null for a primitive slot, or an @CsvSource empty column mapped to a primitive.","commonSituations":"@ParameterizedTest with primitive parameter types combined with null-producing argument sources (@NullSource, @NullAndEmptySource, a @MethodSource that yields null); @CsvFileSource with empty/nullValues cells hitting a primitive param; custom ArgumentAggregator returning null for a primitive slot.","solutions":["Change the primitive parameter to its wrapper type (int -> Integer, boolean -> Boolean) so null is accepted","Remove null-producing argument sources (@NullSource, @NullAndEmptySource) from tests that have primitive parameters","Guard the @MethodSource factory to never emit null for primitive slots","Provide a custom ArgumentConverter (@ConvertWith) that substitutes a sensible default instead of null"],"exampleFix":"// before\n@ParameterizedTest\n@NullSource\nvoid test(int port) { ... }\n\n// after\n@ParameterizedTest\n@NullSource\nvoid test(Integer port) { ... }","handlingStrategy":"validation","validationCode":"// Validate before calling convert\nif (source == null && targetType.isPrimitive()) {\n    // supply a default or switch to the wrapper type\n    return /* default */;\n}\nreturn ConversionSupport.convert(source, targetType, classLoader);","typeGuard":"static boolean canConvertNullTo(Class<?> targetType) {\n    return targetType != null && !targetType.isPrimitive();\n}","tryCatchPattern":null,"preventionTips":["Prefer wrapper types (Integer, Boolean) for parameterized-test parameters that may receive null","Never pair @NullSource or @NullAndEmptySource with primitive parameters","Document which argument sources can emit null"],"tags":["conversion","parameterized-test","null-safety","primitives"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}