{"id":"4da2e860a96acd4d","repo":"junit-team/junit5","slug":"class-must-not-be-null","errorCode":null,"errorMessage":"Class must not be null","messagePattern":"Class must not be null","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-platform-commons/src/main/java/org/junit/platform/commons/logging/LogRecordListener.java","lineNumber":109,"sourceCode":"\t * {@linkplain #logRecordSubmitted submitted} to this listener by the current\n\t * thread for the logger name equal to the name of the given class.\n\t *\n\t * <p>As stated in the Javadoc for {@code LogRecord}, a submitted\n\t * {@code LogRecord} should not be updated by the client application. Thus,\n\t * the {@code LogRecords} in the returned stream should only be inspected for\n\t * testing purposes and not modified in any way.\n\t *\n\t * @param clazz the class for which to get the log records; never {@code null}\n\t * @see #stream()\n\t * @see #stream(Level)\n\t * @see #stream(Class, Level)\n\t */\n\t@SuppressWarnings(\"ConstantValue\")\n\tpublic Stream<LogRecord> stream(Class<?> clazz) {\n\t\t// NOTE: we cannot use org.junit.platform.commons.util.Preconditions here\n\t\t// since that would introduce a package cycle.\n\t\tif (clazz == null) {\n\t\t\tthrow new JUnitException(\"Class must not be null\");\n\t\t}\n\n\t\treturn stream().filter(logRecord -> logRecord.getLoggerName().equals(clazz.getName()));\n\t}\n\n\t/**\n\t * Get a stream of {@link LogRecord log records} that have been\n\t * {@linkplain #logRecordSubmitted submitted} to this listener by the current\n\t * thread for the logger name equal to the name of the given class at the given\n\t * log level.\n\t *\n\t * <p>As stated in the Javadoc for {@code LogRecord}, a submitted\n\t * {@code LogRecord} should not be updated by the client application. Thus,\n\t * the {@code LogRecords} in the returned stream should only be inspected for\n\t * testing purposes and not modified in any way.\n\t *\n\t * @param clazz the class for which to get the log records; never {@code null}\n\t * @param level the log level for which to get the log records; never {@code null}","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-commons/src/main/java/org/junit/platform/commons/logging/LogRecordListener.java#L91-L127","documentation":"LogRecordListener.stream(Class<?>) throws JUnitException when clazz is null, because it calls clazz.getName() to match logger names. This is an @INTERNAL API used by JUnit's own tests.","triggerScenarios":"Calling listener.stream((Class<?>) null).","commonSituations":"Test refactor that passes a null Class reference; parameterization that lost the type token.","solutions":["Pass the concrete class whose logger you want to inspect.","Use stream() and filter records manually if the class is unknown.","Add a nullness check in test utilities that wrap the listener."],"exampleFix":"// before\nlistener.stream(clazz);   // clazz == null\n\n// after\nlistener.stream(MyService.class);","handlingStrategy":"validation","validationCode":"Class<?> clazz = null; // some computed value\nif (clazz == null) {\n    throw new IllegalArgumentException(\"Class must not be null\");\n}\nlistener.stream(clazz);","typeGuard":"static <T> Class<T> requireClass(Class<T> c) {\n    return java.util.Objects.requireNonNull(c, \"Class must not be null\");\n}","tryCatchPattern":"try {\n    listener.stream(clazz);\n} catch (JUnitException e) {\n    if (e.getMessage().equals(\"Class must not be null\")) {\n        // pass a concrete Class instead\n    } else throw e;\n}","preventionTips":["Pass the concrete class literal rather than a possibly-null reference.","Fall back to stream() and filter by logger name manually when the class is unknown.","Keep the internal logging API out of non-JUnit code."],"tags":["junit","platform-commons","logging","internal-api","null-check"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}