{"record":{"id":"3af0c7a1e7de29e6","repo":"alibaba/spring-ai-alibaba","slug":"key-cannot-be-null-or-empty","errorCode":null,"errorMessage":"key cannot be null or empty","messagePattern":"key cannot be null or empty","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":56,"sourceCode":" *\n * @author Spring AI Alibaba\n * @since 1.0.0.3\n */\npublic abstract class BaseStore implements Store {\n\n\t/**\n\t * Validates the putItem parameters.\n\t * @param item the item to validate\n\t */\n\tprotected void validatePutItem(StoreItem item) {\n\t\tif (item == null) {\n\t\t\tthrow new IllegalArgumentException(\"item cannot be null\");\n\t\t}\n\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}","sourceCodeStart":38,"sourceCodeEnd":74,"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#L38-L74","documentation":"validatePutItem requires the item's key to be non-null and non-blank; a key that is null, empty, or whitespace-only causes IllegalArgumentException because the key uniquely identifies the item within its namespace.","triggerScenarios":"Building a StoreItem whose key comes from an empty string, blank user input, or an unset variable, then calling store.putItem(item).","commonSituations":"Key derived from a config field that is empty; trimming user input already done so a whitespace-only key slips through; forgetting to set key in the builder.","solutions":["Provide a non-blank key when constructing StoreItem","Use StringUtils.hasText (Spring) or key != null && !key.isBlank() to validate before putItem","Add builder-level validation so items with blank keys cannot be constructed"],"exampleFix":"// before\nStoreItem item = StoreItem.builder().namespace(ns).key(\"\").value(v).build();\nstore.putItem(item);\n// after\nif (key != null && !key.isBlank()) {\n    store.putItem(StoreItem.builder().namespace(ns).key(key).value(v).build());\n}","handlingStrategy":"validation","validationCode":"if (key == null || key.isBlank()) { throw new IllegalArgumentException(\"key required\"); }","typeGuard":"boolean hasText(String s) { return s != null && !s.isBlank(); }","tryCatchPattern":"try { store.putItem(item); } catch (IllegalArgumentException e) { log.warn(\"Invalid item key: {}\", e.getMessage()); }","preventionTips":["Trim and validate keys derived from user input","Set key explicitly in builders; avoid empty-string defaults","Add builder-level key validation"],"tags":["java","store","validation","key"],"backgroundTag":"empty-required-field","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"}