junit-team/junit5 · error · JUnitException

Class must not be null

Error message

Class must not be null

What it means

Thrown by LoggerFactory.getLogger(Class) when clazz is null. Because LoggerFactory cannot use Preconditions (package cycle), it checks directly and raises JUnitException 'Class must not be null' before constructing a DelegatingLogger with the class name.

Solutions

  1. Pass a concrete Class object such as LoggerFactory.getLogger(MyService.class).
  2. Null-check the Class before calling getLogger and raise a clearer error if absent.
  3. Use the String overload LoggerFactory.getLogger(String) if only a name is available.

Example fix

// before
Logger log = LoggerFactory.getLogger(clazz); // clazz may be null

// after
Logger log = LoggerFactory.getLogger(MyService.class);
Defensive patterns

Strategy: validation

Validate before calling

Class<?> clazz = resolveClass();
if (clazz == null) throw new IllegalArgumentException("Class required");
LoggerFactory.getLogger(clazz);

Prevention

When it happens

Trigger: Calling LoggerFactory.getLogger((Class<?>) null). The `if (clazz == null)` guard fires.

Common situations: Passing a Class reference obtained from a lookup that returned null. Generic helper code losing the class token before calling getLogger. Static initialization where the class field is not yet set.

Related errors


AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11). Data as JSON: /api/errors/392d375ab91c3994. Report an issue: GitHub.

Appendix: source

Thrown at junit-platform-commons/src/main/java/org/junit/platform/commons/logging/LoggerFactory.java:50

	private LoggerFactory() {
		/* no-op */
	}

	private static final Set<LogRecordListener> listeners = ConcurrentHashMap.newKeySet();

	/**
	 * Get a {@link Logger} for the specified class.
	 *
	 * @param clazz the class for which to get the logger; never {@code null}
	 * @return the logger
	 */
	@SuppressWarnings("ConstantValue")
	public static Logger getLogger(Class<?> clazz) {
		// NOTE: we cannot use org.junit.platform.commons.util.Preconditions here
		// since that would introduce a package cycle.
		if (clazz == null) {
			throw new JUnitException("Class must not be null");
		}

		return new DelegatingLogger(clazz.getName());
	}

	/**
	 * Add the supplied {@link LogRecordListener} to the set of registered
	 * listeners.
	 */
	public static void addListener(LogRecordListener listener) {
		listeners.add(listener);
	}

	/**
	 * Remove the supplied {@link LogRecordListener} from the set of registered
	 * listeners.
	 */
	public static void removeListener(LogRecordListener listener) {

View on GitHub (pinned to f070c699a0)