junit-team/junit5 · error · ConversionException

Cannot convert null to primitive value of type ${targetType.

Error message

Cannot convert null to primitive value of type ${targetType.getTypeName()}

What it means

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.

Source

Thrown at junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java:110

	 *
	 * @param source the source {@code String} to convert; may be {@code null}
	 * but only if the target type is a reference type
	 * @param targetType the target type the source should be converted into;
	 * never {@code null}
	 * @param classLoader the {@code ClassLoader} to use; may be {@code null} to
	 * use the default {@code ClassLoader}
	 * @param <T> the type of the target
	 * @return the converted object; may be {@code null} but only if the target
	 * type is a reference type
	 *
	 * @since 1.11
	 */
	@SuppressWarnings("unchecked")
	public static <T> @Nullable T convert(@Nullable String source, Class<T> targetType,
			@Nullable ClassLoader classLoader) {
		if (source == null) {
			if (targetType.isPrimitive()) {
				throw new ConversionException(
					"Cannot convert null to primitive value of type " + targetType.getTypeName());
			}
			return null;
		}

		if (String.class.equals(targetType)) {
			return (T) source;
		}

		Class<?> targetTypeToUse = toWrapperType(targetType);
		Optional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(
			candidate -> candidate.canConvertTo(targetTypeToUse)).findFirst();
		if (converter.isPresent()) {
			try {
				ClassLoader classLoaderToUse = classLoader != null ? classLoader
						: ClassLoaderUtils.getDefaultClassLoader();
				return (T) converter.get().convert(source, targetTypeToUse, classLoaderToUse);
			}

View on GitHub (pinned to 956246301e)

Solutions

  1. Change the parameter to the wrapper type (Integer instead of int) so null is representable.
  2. Configure a non-null emptyValue / nullValue on @CsvSource so blanks become a real value.
  3. Remove @NullSource (or skip that invocation) for primitive parameters.

Example fix

// before
@ParameterizedTest
@NullSource
void test(int n) { }

// after
@ParameterizedTest
@NullSource
void test(Integer n) { }
Defensive patterns

Strategy: type-guard

Validate before calling

// Never attempt null -> primitive. Guard before converting.
Class<?> targetType = int.class;
String source = null;
if (source == null && targetType.isPrimitive()) {
    throw new IllegalArgumentException(
        "Cannot convert null to primitive " + targetType.getTypeName()
        + "; use the wrapper type or supply a non-null value");
}

Type guard

// Choose a representable type for nullable sources.
static Class<?> boxIfPrimitive(Class<?> t) {
    if (!t.isPrimitive()) return t;
    return switch (t.getName()) {
        case "int" -> Integer.class;
        case "long" -> Long.class;
        case "boolean" -> Boolean.class;
        case "double" -> Double.class;
        case "float" -> Float.class;
        case "short" -> Short.class;
        case "byte" -> Byte.class;
        case "char" -> Character.class;
        default -> t;
    };
}

Try / catch

try {
    ConversionSupport.convert(source, targetType, null);
} catch (ConversionException e) {
    if (e.getMessage().contains("Cannot convert null to primitive")) {
        // switch the parameter to the wrapper type or supply a non-null value
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: @NullSource on a primitive parameter; CSV empty/null value targeting a primitive; @MethodSource returning a null for a primitive slot.

Related errors


AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04). Data as JSON: /data/errors/3629b3fd4fa7b97e.json. Report an issue: GitHub.