alibaba/canal · error · NullPointerException

topic cannot null

Error message

topic cannot null

What it means

Thrown by MQUtil.checkTopicWithErr() when the topics varargs array is null or has zero length. This is a pre-condition check before any topic validation logic runs. The exception type is NullPointerException, which is slightly misleading since it is an explicit guard rather than an unexpected null dereference.

Source

Thrown at common/src/main/java/com/alibaba/otter/canal/common/utils/MQUtil.java:52

    /**
     * 判断tag是否是正则
     *
     * @param tag
     * @return
     */
    public static boolean isPatternTag(String tag) {
        return !tag.matches("^[0-9a-zA-Z]+$");
    }

    /**
     * 检查topic有效性
     *
     * @param topics
     */
    public static void checkTopicWithErr(String... topics) {
        if (null == topics || 0 == topics.length) {
            throw new NullPointerException("topic cannot null");
        }

        if (1 == topics.length) {
            boolean ok = checkTopic(topics[0]);
            if (ok) {
                return;
            }
            throw new RuntimeException("topic invalid: " + topics[0]);
        }

        for (String t : topics) {
            if (!checkTopic(t)) {
                throw new IllegalArgumentException("topic invalid: " + t);
            }
            if (isPatternTopic(t)) {
                throw new RuntimeException("pattern topic cannot multi: " + t);
            }
        }

View on GitHub (pinned to 87be50e876)

Solutions

  1. Provide at least one non-null topic string when calling checkTopicWithErr().
  2. Verify canal.mq.topic (or the equivalent config property) is set to a non-empty value in canal.properties or the destination config.
  3. Add a null/empty check before calling the method if the topic source may be absent.

Example fix

// before
canal.mq.topic=
// or programmatic
MQUtil.checkTopicWithErr(); // no topics

// after
canal.mq.topic=example-topic
// programmatic
MQUtil.checkTopicWithErr("example-topic");
Defensive patterns

Strategy: validation

Validate before calling

// Validate topics are non-null and non-empty before calling checkTopicWithErr
if (topics == null || topics.length == 0) {
    throw new IllegalArgumentException("At least one topic must be specified");
}
MQUtil.checkTopicWithErr(topics);

Type guard

null

Try / catch

try {
    MQUtil.checkTopicWithErr(topics);
} catch (NullPointerException e) {
    if ("topic cannot null".equals(e.getMessage())) {
        logger.error("No MQ topic configured. Set canal.mq.topic in canal.properties.");
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling MQUtil.checkTopicWithErr() with no arguments, passing null explicitly, or passing a zero-length String array.

Common situations: Canal MQ configuration has no topic specified (empty or null canal.mq.topic); programmatic MQ producer setup omits the topic parameter; config parsing produces an empty topic list.

Related errors


AI-assisted analysis of alibaba/canal@87be50e876 (2026-08-14). Data as JSON: /api/errors/ff0216a6c8927d9f. Report an issue: GitHub.