{"record":{"id":"e662def7dc844ff7","repo":"TheAlgorithms/Java","slug":"key-cannot-be-null-e662de","errorCode":null,"errorMessage":"Key cannot be null","messagePattern":"Key cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java","lineNumber":238,"sourceCode":"                it.remove();\n                cache.remove(k);\n                notifyEviction(k, entry.value);\n                count++;\n            }\n        }\n\n        return count;\n    }\n\n    /**\n     * Removes the specified key and its associated entry from the cache.\n     *\n     * @param key the key to remove from the cache;\n     * @return the value associated with the key;  or {@code null} if no such key exists\n     */\n    public V removeKey(K key) {\n        if (key == null) {\n            throw new IllegalArgumentException(\"Key cannot be null\");\n        }\n        lock.lock();\n        try {\n            final CacheEntry<V> entry = cache.remove(key);\n            keys.remove(key);\n\n            // No such key in cache\n            if (entry == null) {\n                return null;\n            }\n\n            notifyEviction(key, entry.value);\n            return entry.value;\n        } finally {\n            lock.unlock();\n        }\n    }\n","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/LIFOCache.java#L220-L256","documentation":"Thrown by LIFOCache.removeKey(K) when the key is null. Removal touches both the HashMap and the keys stack; a null key is rejected up front to keep the contract symmetric with get/put. The check precedes lock acquisition.","triggerScenarios":"cache.removeKey(null); cache.removeKey(map.get(absentKey)) yielding null; invalidation loops containing null.","commonSituations":"Invalidation fed by optional IDs; event payloads with unset key fields; tests passing null.","solutions":["Null-check before removing: if (key != null) cache.removeKey(key);","Filter nulls from the key collection before iterating.","Validate keys at the event-ingestion boundary."],"exampleFix":"// before\nfor (K k : keys) cache.removeKey(k);\n// after\nfor (K k : keys) { if (k != null) cache.removeKey(k); }","handlingStrategy":"validation","validationCode":"if (key != null) cache.removeKey(key);","typeGuard":"static <K> boolean isRemovableKey(K key) {\n    return key != null;\n}","tryCatchPattern":null,"preventionTips":["Filter nulls from invalidation key batches.","Validate event-payload key fields at ingestion.","Centralize removal in a helper that skips null."],"tags":["java","lifo-cache","null-check","argument-validation","cache"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}