alibaba/nacos · error · IllegalArgumentException

The parameter 'regex' cannot be null.

Error message

The parameter 'regex' cannot be null.

What it means

Thrown by NamingSelectorFactory.newIpSelector when the regex parameter is null (IllegalArgumentException). The factory requires a non-null regular expression to compile an IP-matching predicate; a null regex has no meaning and would cause a NullPointerException inside Pattern.matches.

Source

Thrown at client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactory.java:103

        if (CollectionUtils.isNotEmpty(clusters)) {
            final Set<String> set = new HashSet<>(clusters);
            Predicate<Instance> filter = instance -> set.contains(instance.getClusterName());
            String clusterString = getUniqueClusterString(clusters);
            return new ClusterSelector(filter, clusterString);
        } else {
            return EMPTY_SELECTOR;
        }
    }
    
    /**
     * Create a IP selector.
     *
     * @param regex regular expression of IP
     * @return IP selector
     */
    public static NamingSelector newIpSelector(String regex) {
        if (regex == null) {
            throw new IllegalArgumentException("The parameter 'regex' cannot be null.");
        }
        return new DefaultNamingSelector(instance -> Pattern.matches(regex, instance.getIp()));
    }
    
    /**
     * Create a metadata selector.
     *
     * @param metadata metadata that needs to be matched
     * @return metadata selector
     */
    public static NamingSelector newMetadataSelector(Map<String, String> metadata) {
        return newMetadataSelector(metadata, false);
    }
    
    /**
     * Create a metadata selector.
     *
     * @param metadata target metadata

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Pass a non-null regex string, even an empty string or a catch-all like ".*" if all IPs should match.
  2. Null-check the regex variable before calling newIpSelector and provide a default.
  3. Review the source of the regex value (config property, user input) to ensure it is never null.

Example fix

// before
NamingSelector selector = NamingSelectorFactory.newIpSelector(regexFromConfig);

// after
String regex = regexFromConfig != null ? regexFromConfig : ".*";
NamingSelector selector = NamingSelectorFactory.newIpSelector(regex);
Defensive patterns

Strategy: validation

Validate before calling

String safeRegex = (regex != null) ? regex : ".*";
NamingSelector selector = NamingSelectorFactory.newIpSelector(safeRegex);

Prevention

When it happens

Trigger: Calling NamingSelectorFactory.newIpSelector(null). The null check is a fail-fast guard before the regex is used in a Pattern.matches call.

Common situations: Passing a nullable variable as the regex without null-checking; configuration-driven regex that resolves to null; copy-paste error omitting the regex argument.

Related errors


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