{"record":{"id":"1d66c4c5157ac05b","repo":"TheAlgorithms/Java","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":"src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java","lineNumber":231,"sourceCode":"            if (entry != null && entry.getValue().isExpired()) {\n                it.remove();\n                notifyEviction(entry.getKey(), entry.getValue().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        CacheEntry<V> entry = cache.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    }\n\n    /**\n     * Notifies the eviction listener, if one is registered, that a key-value pair has been evicted.\n     *\n     * <p>If the {@code evictionListener} is not {@code null}, it is invoked with the provided key\n     * and value. Any exceptions thrown by the listener are caught and logged to standard error,\n     * preventing them from disrupting cache operations.","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java#L213-L249","documentation":"Thrown by FIFOCache.removeKey(K) when the key is null. Removal uses the underlying HashMap.remove, and a null key is rejected to keep the contract symmetric with get/put. The check precedes lock acquisition, so a rejected removal leaves cache state unchanged.","triggerScenarios":"cache.removeKey(null); cache.removeKey(map.get(absentKey)) where the inner lookup yields null; cleanup loops driven by a collection that contains null.","commonSituations":"Invalidation routines fed by optional IDs; event handlers where the payload key field is unset; tests that pass null to assert behavior.","solutions":["Null-check the key before removing: if (key != null) cache.removeKey(key);","Filter nulls out of the collection of keys to invalidate 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 out of invalidation key batches before iterating.","Validate event-payload key fields at ingestion.","Centralize removal in a helper that silently skips null."],"tags":["java","fifo-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"}