{"record":{"id":"901dc05f483ef576","repo":"TheAlgorithms/Java","slug":"key-must-not-be-null","errorCode":null,"errorMessage":"Key must not be null","messagePattern":"Key must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java","lineNumber":115,"sourceCode":"        this.evictionListener = builder.evictionListener;\n        this.evictionStrategy = builder.evictionStrategy;\n    }\n\n    /**\n     * Retrieves the value associated with the specified key from the cache.\n     *\n     * <p>If the key is not present or the corresponding entry has expired, this method\n     * returns {@code null}. If an expired entry is found, it will be removed and the\n     * eviction listener (if any) will be notified. Cache hit-and-miss statistics are\n     * also updated accordingly.\n     *\n     * @param key the key whose associated value is to be returned; must not be {@code null}\n     * @return the cached value associated with the key, or {@code null} if not present or expired\n     * @throws IllegalArgumentException if {@code key} is {@code null}\n     */\n    public V get(K key) {\n        if (key == null) {\n            throw new IllegalArgumentException(\"Key must not be null\");\n        }\n\n        lock.lock();\n        try {\n            evictionStrategy.onAccess(this);\n\n            CacheEntry<V> entry = cache.get(key);\n            if (entry == null || entry.isExpired()) {\n                if (entry != null) {\n                    cache.remove(key);\n                    notifyEviction(key, entry.value);\n                }\n                misses++;\n                return null;\n            }\n            hits++;\n            return entry.value;\n        } finally {","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java#L97-L133","documentation":"Thrown by FIFOCache.get(K) when the key argument is null. The cache keys a HashMap internally, and null keys collide with the 'absent' return contract, so the API rejects null up front before acquiring the lock. Statistics and eviction-strategy callbacks are also skipped on rejection.","triggerScenarios":"cache.get(null); cache.get(map.get(\"missingKey\")) where the inner get returns null; a lookup driven by user input that was not validated for presence.","commonSituations":"Web request parameters mapped directly to cache keys without a presence check; optional fields deserialized as null; migrating from a cache that tolerated null keys.","solutions":["Validate the key is non-null before calling get(), returning a default or a 404 upstream.","Use Optional.ofNullable(key).map(cache::get).orElse(null).","Guard at the controller/service boundary so null never reaches the cache layer."],"exampleFix":"// before\nV v = cache.get(request.getKey());\n// after\nK k = request.getKey();\nif (k == null) return defaultValue;\nV v = cache.get(k);","handlingStrategy":"validation","validationCode":"if (key == null) return defaultValue;\nV value = cache.get(key);","typeGuard":"static <K> boolean isLookupKey(K key) {\n    return key != null;\n}","tryCatchPattern":null,"preventionTips":["Validate request parameters at the controller before they reach the cache.","Wrap lookups in a helper that returns Optional and centralizes the null check.","Never pass the result of Map.get() straight into cache.get() without a null check."],"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"}