alibaba/nacos · error · IllegalArgumentException

listener is null

Error message

listener is null

What it means

Thrown by CacheData.addListener when the passed Listener is null. CacheData holds per-config listeners and must wrap each in a ManagerListenerWrap; a null listener would cause an NPE later in the notify path, so it is rejected immediately. This is a programmer error, not a runtime/environment condition.

Source

Thrown at client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java:242

        this.lastModifiedTs.set(lastModifiedTs);
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    /**
     * Add listener if CacheData already set new content, Listener should init lastCallMd5 by CacheData.md5
     *
     * @param listener listener
     */
    public void addListener(Listener listener) throws NacosException {
        if (null == listener) {
            throw new IllegalArgumentException("listener is null");
        }
        ManagerListenerWrap wrap;
        if (listener instanceof AbstractConfigChangeListener) {
            ConfigResponse cr = new ConfigResponse();
            cr.setDataId(dataId);
            cr.setGroup(group);
            cr.setContent(content);
            cr.setEncryptedDataKey(encryptedDataKey);
            configFilterChainManager.doFilter(null, cr);
            String contentTmp = cr.getContent();
            wrap = new ManagerListenerWrap(listener, md5, contentTmp);
        } else {
            wrap = new ManagerListenerWrap(listener, md5);
        }
        
        if (listeners.addIfAbsent(wrap)) {
            LOGGER.info("[{}] [add-listener] ok, tenant={}, dataId={}, group={}, cnt={}", envName,
                tenant, dataId,

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Null-check the listener before calling addListener and log/skip if absent.
  2. Ensure the listener is constructed unconditionally before registration.
  3. Use a defensive helper that wraps addListener with a null guard.

Example fix

// before
cacheData.addListener(maybeNullListener);

// after
if (listener != null) {
    cacheData.addListener(listener);
} else {
    log.warn("Skipping listener registration: listener is null");
}
Defensive patterns

Strategy: validation

Validate before calling

if (listener == null) {
    throw new IllegalArgumentException("listener must not be null");
}
cacheData.addListener(listener);

Type guard

static boolean isRegistrableListener(Listener l) {
    return l != null;
}

Try / catch

try {
    cacheData.addListener(listener);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("listener is null")) {
        log.warn("listener was null, skipping registration");
    }
}

Prevention

When it happens

Trigger: Calling cacheData.addListener(null) directly, or passing a listener variable that was never initialized / was set to null by a failed factory call.

Common situations: Listener reference null due to a missing null-check after construction, conditional logic that skips listener creation but still calls addListener, or a copy-paste error.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/07dfb8b848700d88. Report an issue: GitHub.