{"record":{"id":"c91b85e0e9396fa9","repo":"alibaba/spring-ai-alibaba","slug":"key-cannot-be-null","errorCode":null,"errorMessage":"key cannot be null","messagePattern":"key cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/store/stores/BaseStore.java","lineNumber":70,"sourceCode":"\t\tif (item.getNamespace() == null) {\n\t\t\tthrow new IllegalArgumentException(\"namespace cannot be null\");\n\t\t}\n\t\tif (item.getKey() == null || item.getKey().trim().isEmpty()) {\n\t\t\tthrow new IllegalArgumentException(\"key cannot be null or empty\");\n\t\t}\n\t}\n\n\t/**\n\t * Validates the getItem parameters.\n\t * @param namespace namespace\n\t * @param key key\n\t */\n\tprotected void validateGetItem(List<String> namespace, String key) {\n\t\tif (namespace == null) {\n\t\t\tthrow new IllegalArgumentException(\"namespace cannot be null\");\n\t\t}\n\t\tif (key == null) {\n\t\t\tthrow new IllegalArgumentException(\"key cannot be null\");\n\t\t}\n\t\tif (key.trim().isEmpty()) {\n\t\t\tthrow new IllegalArgumentException(\"key cannot be empty\");\n\t\t}\n\t}\n\n\t/**\n\t * Validates the deleteItem parameters.\n\t * @param namespace namespace\n\t * @param key key\n\t */\n\tprotected void validateDeleteItem(List<String> namespace, String key) {\n\t\tif (namespace == null) {\n\t\t\tthrow new IllegalArgumentException(\"namespace cannot be null\");\n\t\t}\n\t\tif (key == null) {\n\t\t\tthrow new IllegalArgumentException(\"key cannot be null\");\n\t\t}","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/store/stores/BaseStore.java#L52-L88","documentation":"BaseStore.validateGetItem rejects a null key when reading a store item. The store requires both a namespace and a non-null, non-empty key to locate an item, so a null key is an invalid argument for getItem. This guards subclasses against constructing malformed storage lookups.","triggerScenarios":"Calling store.getItem(namespace, null) or passing a variable that is null (e.g. a map lookup result or an unset field) as the key argument to getItem on any BaseStore subclass.","commonSituations":"Extracting the key from user input, JSON payloads, or a Map.get() that returns null when the entry is absent; refactoring code that renamed a config field so the key variable is no longer populated.","solutions":["Ensure a non-null key is passed to getItem; check the value before the call.","If the key comes from a Map/JSON lookup, use getOrDefault or validate presence first.","Wrap the call in try-catch for IllegalArgumentException if null keys are possible and handle them explicitly."],"exampleFix":"// before\nstore.getItem(List.of(\"users\"), params.get(\"key\"));\n// after\nString key = params.get(\"key\");\nif (key == null || key.isBlank()) {\n    throw new IllegalArgumentException(\"item key is required\");\n}\nstore.getItem(List.of(\"users\"), key);","handlingStrategy":"validation","validationCode":"if (key == null || key.isBlank()) {\n    throw new IllegalArgumentException(\"store item key is required\");\n}\nstore.getItem(namespace, key);","typeGuard":"boolean hasKey(String k) { return k != null && !k.isBlank(); }","tryCatchPattern":"try {\n    store.getItem(namespace, key);\n} catch (IllegalArgumentException e) {\n    // key/namespace invalid\n    log.warn(\"Invalid store lookup: {}\", e.getMessage());\n}","preventionTips":["Validate keys at the API boundary before they reach the store layer.","Never pass Map.get() results directly as keys; check presence first.","Use Optional<String> for possibly-absent keys and unwrap explicitly."],"tags":["java","validation","null-argument","store"],"backgroundTag":"null-argument","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}