{"record":{"id":"d7e67a85ff22da41","repo":"TheAlgorithms/Java","slug":"key-must-not-be-null-d7e67a","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/RRCache.java","lineNumber":114,"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                    removeKey(key);\n                    notifyEviction(key, entry.value);\n                }\n                misses++;\n                return null;\n            }\n            hits++;\n            return entry.value;\n        } finally {","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/RRCache.java#L96-L132","documentation":"RRCache.get() rejects null keys immediately (before acquiring the lock) with IllegalArgumentException. The cache uses a HashMap and a parallel key-tracking ArrayList; null keys would break consistency between these structures and the random-eviction selection.","triggerScenarios":"Calling cache.get(null) directly, or passing a key variable whose value is null — typically the result of a map lookup miss, a database query returning null, or an unboxed Optional that was empty.","commonSituations":"Key derived from a nullable entity field or an external API response. Deserialization producing null keys. Code paths where the key source legitimately returns null on 'not found' but the caller does not handle it.","solutions":["Null-check the key before calling get() and handle the absent case","Filter out null keys at the data source before they reach the cache","Use Optional to make key nullability explicit in the calling pipeline"],"exampleFix":"// before\nString id = request.getParameter(\"id\"); // may be null\nbyte[] data = cache.get(id); // throws if id is null\n\n// after\nString id = request.getParameter(\"id\");\nbyte[] data = (id != null) ? cache.get(id) : null;","handlingStrategy":"validation","validationCode":"public <V> V getFromCache(RRCache<String,V> cache, String key) {\n    if (key == null) return null; // or handle absence explicitly\n    return cache.get(key);\n}","typeGuard":"// Optional-based guard to make null explicit\nOptional<String> safeKey = Optional.ofNullable(rawKey);\nV result = safeKey.map(cache::get).orElse(null);","tryCatchPattern":"try {\n    return cache.get(key);\n} catch (IllegalArgumentException e) {\n    if (key == null) return null; // graceful absent handling\n    throw e;\n}","preventionTips":["Null-check keys before any cache interaction","Use Optional in method signatures to signal nullable keys","Filter null keys at the data-ingestion boundary"],"tags":["rr-cache","null-safety","validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}