{"record":{"id":"be73737e26da8319","repo":"apache/rocketmq","slug":"select-message-queue-return-null","errorCode":null,"errorMessage":"select message queue return null.","messagePattern":"select message queue return null\\.","errorType":"exception","errorClass":"MQClientException","httpStatus":null,"severity":"error","filePath":"client/src/main/java/org/apache/rocketmq/client/impl/producer/DefaultMQProducerImpl.java","lineNumber":712,"sourceCode":"                List<MessageQueue> messageQueueList =\n                        mQClientFactory.getMQAdminImpl().parsePublishMessageQueues(topicPublishInfo.getMessageQueueList());\n                Message userMessage = MessageAccessor.cloneMessage(msg);\n                String userTopic = NamespaceUtil.withoutNamespace(userMessage.getTopic(), mQClientFactory.getClientConfig().getNamespace());\n                userMessage.setTopic(userTopic);\n\n                mq = mQClientFactory.getClientConfig().queueWithNamespace(selector.select(messageQueueList, userMessage, arg));\n            } catch (Throwable e) {\n                throw new MQClientException(\"select message queue threw exception.\", e);\n            }\n\n            long costTime = System.currentTimeMillis() - beginStartTime;\n            if (timeout < costTime) {\n                throw new RemotingTooMuchRequestException(\"sendSelectImpl call timeout\");\n            }\n            if (mq != null) {\n                return mq;\n            } else {\n                throw new MQClientException(\"select message queue return null.\", null);\n            }\n        }\n\n        validateNameServerSetting();\n        throw new MQClientException(\"No route info for this topic, \" + msg.getTopic(), null);\n    }\n\n    public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName, final boolean resetIndex) {\n        return this.mqFaultStrategy.selectOneMessageQueue(tpInfo, lastBrokerName, resetIndex);\n    }\n\n    public void updateFaultItem(final String brokerName, final long currentLatency, boolean isolation,\n                                boolean reachable) {\n        this.mqFaultStrategy.updateFaultItem(brokerName, currentLatency, isolation, reachable);\n    }\n\n    private void validateNameServerSetting() throws MQClientException {\n        List<String> nsList = this.getMqClientFactory().getMQClientAPIImpl().getNameServerAddressList();","sourceCodeStart":694,"sourceCodeEnd":730,"githubUrl":"https://github.com/apache/rocketmq/blob/293f5885719fc4aa3619446a1900f58ccfcfdd29/client/src/main/java/org/apache/rocketmq/client/impl/producer/DefaultMQProducerImpl.java#L694-L730","documentation":"MQClientException thrown from invokeMessageQueueSelector when the MessageQueueSelector.select(...) completes without throwing but returns null (the client also null-checks the namespace-wrapped result). A null selection cannot be mapped to a broker queue, so the send-select call aborts before any network I/O. Distinct from error 267: here the selector ran cleanly but produced no queue.","triggerScenarios":"triggerScenarios","commonSituations":"commonSituations","solutions":["Make the selector always return a concrete queue; fall back to a default strategy (round-robin index or mqs.get(0)) instead of null","If the lookup can legitimately miss, seed/initialize the mapping before returning or fall back to a hash of the message key","Handle the empty-mqs case explicitly (throw a descriptive exception or pick queue 0) rather than letting the lambda fall through to null","Add a unit test asserting select() never returns null for all expected inputs"],"exampleFix":"// before\nMessageQueueSelector s = (mqs, m, arg) -> keyQueueMap.get(arg); // null on miss\n// after\nMessageQueueSelector s = (mqs, m, arg) -> {\n    MessageQueue mq = keyQueueMap.get(arg);\n    return mq != null ? mq : mqs.get((arg.hashCode() & Integer.MAX_VALUE) % mqs.size());\n};","handlingStrategy":"validation","validationCode":" MessageQueueSelector nonNull = (mqs, m, arg) -> {\n    MessageQueue q = rawSelector.select(mqs, m, arg);\n    return q != null ? q : mqs.get(ThreadLocalRandom.current().nextInt(mqs.size()));\n};","typeGuard":" boolean selectsQueue(MessageQueueSelector s, List<MessageQueue> mqs, Message m, Object arg) {\n    return s.select(mqs, m, arg) != null; // for pre-flight checks in tests\n}","tryCatchPattern":"try {\n    producer.send(msg, selector, arg);\n} catch (MQClientException e) {\n    if (\"select message queue return null.\".equals(e.getMessage())) {\n        producer.send(msg); // fallback: default send-path selection\n    } else throw e;\n}","preventionTips":["Always give selectors a terminal fallback branch (round-robin or hash)","Avoid map.get(...)/Optional.orElse(null) as the return expression of select()","Assert non-null return in selector unit tests for every input class"],"tags":["rocketmq","producer","message-queue-selector","null-return"],"backgroundTag":null,"analyzedSha":"293f5885719fc4aa3619446a1900f58ccfcfdd29","analyzedAt":"2026-08-14T11:50:13.822Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}