{"record":{"id":"215cf7f19953e28e","repo":"alibaba/nacos","slug":"the-parameter-metadata-cannot-be-null","errorCode":null,"errorMessage":"The parameter 'metadata' cannot be null.","messagePattern":"The parameter 'metadata' 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":127,"sourceCode":"     * 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\n     * @param isAny    true if any of the metadata needs to be matched, false if all the metadata need to be matched.\n     * @return metadata selector\n     */\n    public static NamingSelector newMetadataSelector(Map<String, String> metadata, boolean isAny) {\n        if (metadata == null) {\n            throw new IllegalArgumentException(\"The parameter 'metadata' cannot be null.\");\n        }\n        \n        Predicate<Instance> filter = instance -> instance.getMetadata().size() >= metadata.size();\n        \n        for (Map.Entry<String, String> entry : metadata.entrySet()) {\n            Predicate<Instance> nextFilter = instance -> {\n                Map<String, String> map = instance.getMetadata();\n                return Objects.equals(map.get(entry.getKey()), entry.getValue());\n            };\n            if (isAny) {\n                filter = filter.or(nextFilter);\n            } else {\n                filter = filter.and(nextFilter);\n            }\n        }\n        return new DefaultNamingSelector(filter);\n    }\n    ","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/client/src/main/java/com/alibaba/nacos/client/naming/selector/NamingSelectorFactory.java#L109-L145","documentation":"Thrown by NamingSelectorFactory.newMetadataSelector when the metadata map is null (IllegalArgumentException). The selector factory iterates the metadata entries to build a predicate chain; a null map would cause a NullPointerException in the loop.","triggerScenarios":"Calling NamingSelectorFactory.newMetadataSelector(null) or newMetadataSelector(null, isAny). The null check guards the entry-set iteration.","commonSituations":"Passing a nullable metadata map from a config or request object without null-checking; building a selector from optional metadata that may be absent.","solutions":["Pass a non-null Map (use Collections.emptyMap() if no metadata filtering is needed).","Null-check the metadata variable and skip selector creation or use a default empty map.","Review the data source for the metadata map to ensure it is initialized."],"exampleFix":"// before\nNamingSelector selector = NamingSelectorFactory.newMetadataSelector(meta);\n\n// after\nMap<String, String> meta = optionalMeta != null ? optionalMeta : Collections.emptyMap();\nNamingSelector selector = NamingSelectorFactory.newMetadataSelector(meta);","handlingStrategy":"validation","validationCode":"Map<String, String> safeMeta = (metadata != null) ? metadata : Collections.emptyMap();\nNamingSelector selector = NamingSelectorFactory.newMetadataSelector(safeMeta, isAny);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a non-null metadata map; use Collections.emptyMap() as a default.","Validate optional metadata from config or request objects before selector creation.","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"}