TheAlgorithms/Java · error · IllegalArgumentException
Key must not be null
Error message
Key must not be null
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java:115
this.evictionListener = builder.evictionListener;
this.evictionStrategy = builder.evictionStrategy;
}
/**
* Retrieves the value associated with the specified key from the cache.
*
* <p>If the key is not present or the corresponding entry has expired, this method
* returns {@code null}. If an expired entry is found, it will be removed and the
* eviction listener (if any) will be notified. Cache hit-and-miss statistics are
* also updated accordingly.
*
* @param key the key whose associated value is to be returned; must not be {@code null}
* @return the cached value associated with the key, or {@code null} if not present or expired
* @throws IllegalArgumentException if {@code key} is {@code null}
*/
public V get(K key) {
if (key == null) {
throw new IllegalArgumentException("Key must not be null");
}
lock.lock();
try {
evictionStrategy.onAccess(this);
CacheEntry<V> entry = cache.get(key);
if (entry == null || entry.isExpired()) {
if (entry != null) {
cache.remove(key);
notifyEviction(key, entry.value);
}
misses++;
return null;
}
hits++;
return entry.value;
} finally {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before V v = cache.get(request.getKey()); // after K k = request.getKey(); if (k == null) return defaultValue; V v = cache.get(k);
Defensive patterns
Strategy: validation
Validate before calling
if (key == null) return defaultValue; V value = cache.get(key);
Type guard
static <K> boolean isLookupKey(K key) {
return key != null;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Key and value must not be null
- Key cannot be null
- Listener must not be null
- Eviction strategy must not be null
- TTL must be >= 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/901dc05f483ef576.
Report an issue: GitHub.