{"record":{"id":"088d30e05e3347fa","repo":"apache/kafka","slug":"rebalancelistener-cannot-be-null","errorCode":null,"errorMessage":"RebalanceListener cannot be null","messagePattern":"RebalanceListener cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java","lineNumber":194,"sourceCode":"            .filter(tp -> !lost.contains(tp))\n            .collect(Collectors.toSet());\n        this.subscriptions.assignFromSubscribed(remaining);\n    }\n\n    @Override\n    public synchronized Set<String> subscription() {\n        return subscriptions.subscription();\n    }\n\n    @Override\n    public synchronized void subscribe(Collection<String> topics) {\n        subscribeInternal(topics, null);\n    }\n\n    @Override\n    public synchronized void subscribe(Pattern pattern, final ConsumerRebalanceListener listener) {\n        if (listener == null)\n            throw new IllegalArgumentException(\"RebalanceListener cannot be null\");\n\n        subscribeInternal(pattern, listener);\n    }\n\n    @Override\n    public synchronized void subscribe(Pattern pattern) {\n        subscribeInternal(pattern, null);\n    }\n\n    @Override\n    public synchronized void subscribe(SubscriptionPattern pattern, ConsumerRebalanceListener listener) {\n        if (listener == null)\n            throw new IllegalArgumentException(\"RebalanceListener cannot be null\");\n\n        subscribeInternal(pattern, listener);\n    }\n\n    @Override","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java#L176-L212","documentation":"Thrown by MockConsumer.subscribe(Pattern, ConsumerRebalanceListener) when the listener argument is null. This overload explicitly takes a listener (unlike the no-listener overloads that pass null internally), and Kafka requires the listener because rebalance callbacks are the whole point of supplying one. If you do not need callbacks, call the single-arg subscribe(Pattern) overload instead.","triggerScenarios":"Passing a null rebalance listener to the (Pattern, listener) overload; passing a listener variable that was never initialized; copy-paste from code that conditionally supplies a listener.","commonSituations":"Tests that subscribe with a stub listener field that defaulted to null; refactor that removed listener initialization but kept the two-arg call.","solutions":["Pass a real ConsumerRebalanceListener (e.g. a no-op stub) when calling the two-arg overload.","If you have no callbacks, switch to the single-arg subscribe(Pattern) overload.","Initialize the listener field before subscribing; assert it is non-null in a @BeforeEach."],"exampleFix":"// before\nmockConsumer.subscribe(Pattern.compile(\"orders-.*\"), null);\n\n// after\n// option A: no listener needed\nmockConsumer.subscribe(Pattern.compile(\"orders-.*\"));\n// option B: supply a stub\nmockConsumer.subscribe(Pattern.compile(\"orders-.*\"), new ConsumerRebalanceListener() {\n    @Override public void onPartitionsRevoked(Collection<TopicPartition> p) {}\n    @Override public void onPartitionsAssigned(Collection<TopicPartition> p) {}\n});","handlingStrategy":"validation","validationCode":"ConsumerRebalanceListener listener = Objects.requireNonNull(listenerArg, \"listener\");\nmockConsumer.subscribe(pattern, listener);","typeGuard":"static boolean hasListener(ConsumerRebalanceListener l) {\n    return l != null;\n}","tryCatchPattern":"try {\n    mockConsumer.subscribe(pattern, listener);\n} catch (IllegalArgumentException e) {\n    if (\"RebalanceListener cannot be null\".equals(e.getMessage())) {\n        mockConsumer.subscribe(pattern, new NoOpConsumerRebalanceListener()); // or use single-arg overload\n    } else throw e;\n}","preventionTips":["Initialize the listener field in @BeforeEach.","If no callbacks are needed, prefer the single-arg subscribe(Pattern) overload.","Keep a NoOpConsumerRebalanceListener stub available for tests."],"tags":["consumer","mock-consumer","test","subscribe","null-check"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}