{"record":{"id":"c4033ff33aee6a0f","repo":"apache/kafka","slug":"topic-pattern-cannot-be-null-empty","errorCode":null,"errorMessage":"Topic pattern cannot be {null|empty}","messagePattern":"Topic pattern cannot be (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java","lineNumber":227,"sourceCode":"        subscribeInternal(pattern, listener);\n    }\n\n    @Override\n    public synchronized void subscribe(SubscriptionPattern pattern) {\n        subscribeInternal(pattern, null);\n    }\n\n    @Override\n    public void subscribe(Collection<String> topics, final ConsumerRebalanceListener listener) {\n        if (listener == null)\n            throw new IllegalArgumentException(\"RebalanceListener cannot be null\");\n\n        subscribeInternal(topics, listener);\n    }\n\n    private synchronized void subscribeInternal(SubscriptionPattern pattern, ConsumerRebalanceListener listener) {\n        if (pattern == null || pattern.toString().isEmpty())\n            throw new IllegalArgumentException(\"Topic pattern cannot be \" + (pattern == null ? \"null\" : \"empty\"));\n\n        ensureNotClosed();\n        committed.clear();\n        if (listener != null)\n            subscriptions.setRebalanceListener(listener, this);\n        subscriptions.subscribe(pattern);\n    }\n\n    private synchronized void subscribeInternal(Collection<String> topics, ConsumerRebalanceListener listener) {\n        ensureNotClosed();\n        committed.clear();\n        if (listener != null)\n            subscriptions.setRebalanceListener(listener, this);\n        subscriptions.subscribe(new HashSet<>(topics));\n    }\n\n    private synchronized void subscribeInternal(Pattern pattern, ConsumerRebalanceListener listener) {\n        ensureNotClosed();","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java#L209-L245","documentation":"Thrown by MockConsumer.subscribeInternal(SubscriptionPattern, listener) when the pattern is null or its toString() is empty. The SubscriptionPattern drives topic matching, and an empty/null pattern would match nothing meaningfully and is almost always a config bug. The message tells you which of the two (null vs empty) was the case.","triggerScenarios":"Calling subscribe with a null SubscriptionPattern; passing a custom SubscriptionPattern whose toString() returns \"\"; constructing a SubscriptionPattern from an empty input string; refactor that drops pattern construction but keeps the subscribe call.","commonSituations":"Tests building SubscriptionPattern from externalized config that was unset; custom SubscriptionPattern implementations with a buggy toString; helpers that return null when no pattern is configured.","solutions":["Ensure the SubscriptionPattern is non-null and its toString() yields a non-empty expression before subscribing.","Build the SubscriptionPattern from a validated non-empty source string; fail fast at startup if the source is blank.","If using a custom SubscriptionPattern class, override toString() to return the underlying pattern, not an empty string."],"exampleFix":"// before\nmockConsumer.subscribe(null, listener);\n// or\nmockConsumer.subscribe(new MySubscriptionPattern(\"\"), listener);\n\n// after\nSubscriptionPattern pattern = SubscriptionPattern.compile(\"orders-.*\");\nmockConsumer.subscribe(pattern, listener);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(pattern, \"SubscriptionPattern\");\nif (pattern.toString() == null || pattern.toString().isEmpty())\n    throw new IllegalArgumentException(\"SubscriptionPattern must be non-empty\");\nmockConsumer.subscribe(pattern, listener);","typeGuard":"static boolean isUsablePattern(SubscriptionPattern p) {\n    return p != null && p.toString() != null && !p.toString().isEmpty();\n}","tryCatchPattern":"try {\n    mockConsumer.subscribe(pattern, listener);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Topic pattern cannot be\")) {\n        // load a validated default pattern and retry\n    }\n    throw e;\n}","preventionTips":["Build SubscriptionPattern from a validated non-empty source string at startup.","For custom SubscriptionPattern classes, override toString() to return the underlying pattern.","Add a startup assertion that the configured pattern string is non-blank."],"tags":["consumer","mock-consumer","test","subscribe","validation","subscription-pattern"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}