{"id":"3629b3fd4fa7b97e","repo":"junit-team/junit5","slug":"cannot-convert-null-to-primitive-value-of-type-t-3629b3","errorCode":null,"errorMessage":"Cannot convert null to primitive value of type ${targetType.getTypeName()}","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/956246301e03d757539f7b76dd5a91f298637563/junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java#L92-L128","documentation":"ConversionSupport.convert throws ConversionException when source is null and targetType is a primitive (int, long, boolean, ...). Primitives cannot represent null, so the conversion is impossible.","triggerScenarios":"A null argument (from @NullSource, an empty CSV cell, or a null object) being converted to a primitive parameter - e.g. an @CsvSource blank value mapped to an `int` parameter.","commonSituations":"@NullSource on a primitive parameter; CSV empty/null value targeting a primitive; @MethodSource returning a null for a primitive slot.","solutions":["Change the parameter to the wrapper type (Integer instead of int) so null is representable.","Configure a non-null emptyValue / nullValue on @CsvSource so blanks become a real value.","Remove @NullSource (or skip that invocation) for primitive parameters."],"exampleFix":"// before\n@ParameterizedTest\n@NullSource\nvoid test(int n) { }\n\n// after\n@ParameterizedTest\n@NullSource\nvoid test(Integer n) { }","handlingStrategy":"type-guard","validationCode":"// Never attempt null -> primitive. Guard before converting.\nClass<?> targetType = int.class;\nString source = null;\nif (source == null && targetType.isPrimitive()) {\n    throw new IllegalArgumentException(\n        \"Cannot convert null to primitive \" + targetType.getTypeName()\n        + \"; use the wrapper type or supply a non-null value\");\n}","typeGuard":"// Choose a representable type for nullable sources.\nstatic Class<?> boxIfPrimitive(Class<?> t) {\n    if (!t.isPrimitive()) return t;\n    return switch (t.getName()) {\n        case \"int\" -> Integer.class;\n        case \"long\" -> Long.class;\n        case \"boolean\" -> Boolean.class;\n        case \"double\" -> Double.class;\n        case \"float\" -> Float.class;\n        case \"short\" -> Short.class;\n        case \"byte\" -> Byte.class;\n        case \"char\" -> Character.class;\n        default -> t;\n    };\n}","tryCatchPattern":"try {\n    ConversionSupport.convert(source, targetType, null);\n} catch (ConversionException e) {\n    if (e.getMessage().contains(\"Cannot convert null to primitive\")) {\n        // switch the parameter to the wrapper type or supply a non-null value\n    } else throw e;\n}","preventionTips":["Use wrapper types (Integer, Long, ...) for parameters that may receive null.","Configure @CsvSource nullValue/emptyValue so blanks map to a real primitive value.","Never combine @NullSource with a primitive parameter."],"tags":["junit","platform-commons","conversion","primitive","null"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}