{"record":{"id":"07c3292f3f8f9ab1","repo":"alibaba/nacos","slug":"the-parameter-regex-cannot-be-null","errorCode":null,"errorMessage":"The parameter 'regex' cannot be null.","messagePattern":"The parameter 'regex' cannot be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactory.java","lineNumber":103,"sourceCode":"        if (CollectionUtils.isNotEmpty(clusters)) {\n            final Set<String> set = new HashSet<>(clusters);\n            Predicate<Instance> filter = instance -> set.contains(instance.getClusterName());\n            String clusterString = getUniqueClusterString(clusters);\n            return new ClusterSelector(filter, clusterString);\n        } else {\n            return EMPTY_SELECTOR;\n        }\n    }\n    \n    /**\n     * Create a IP selector.\n     *\n     * @param regex regular expression of IP\n     * @return IP selector\n     */\n    public static NamingSelector newIpSelector(String regex) {\n        if (regex == null) {\n            throw new IllegalArgumentException(\"The parameter 'regex' cannot be null.\");\n        }\n        return new DefaultNamingSelector(instance -> Pattern.matches(regex, instance.getIp()));\n    }\n    \n    /**\n     * Create a metadata selector.\n     *\n     * @param metadata metadata that needs to be matched\n     * @return metadata selector\n     */\n    public static NamingSelector newMetadataSelector(Map<String, String> metadata) {\n        return newMetadataSelector(metadata, false);\n    }\n    \n    /**\n     * Create a metadata selector.\n     *\n     * @param metadata target metadata","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactory.java#L85-L121","documentation":"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.","triggerScenarios":"Calling NamingSelectorFactory.newIpSelector(null). The null check is a fail-fast guard before the regex is used in a Pattern.matches call.","commonSituations":"Passing a nullable variable as the regex without null-checking; configuration-driven regex that resolves to null; copy-paste error omitting the regex argument.","solutions":["Pass a non-null regex string, even an empty string or a catch-all like \".*\" if all IPs should match.","Null-check the regex variable before calling newIpSelector and provide a default.","Review the source of the regex value (config property, user input) to ensure it is never null."],"exampleFix":"// before\nNamingSelector selector = NamingSelectorFactory.newIpSelector(regexFromConfig);\n\n// after\nString regex = regexFromConfig != null ? regexFromConfig : \".*\";\nNamingSelector selector = NamingSelectorFactory.newIpSelector(regex);","handlingStrategy":"validation","validationCode":"String safeRegex = (regex != null) ? regex : \".*\";\nNamingSelector selector = NamingSelectorFactory.newIpSelector(safeRegex);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a non-null regex to newIpSelector; use \".*\" as a catch-all default.","Validate user-provided or config-driven regex values before passing to the factory.","Centralize selector creation in a utility method that enforces non-null arguments."],"tags":["naming","selector","validation","null-check","client"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}