TheAlgorithms/Java · error · IllegalArgumentException
Key must not be null
Error message
Key must not be null
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/RRCache.java:114
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) {
removeKey(key);
notifyEviction(key, entry.value);
}
misses++;
return null;
}
hits++;
return entry.value;
} finally {View on GitHub (pinned to fdfb9a395b)
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
Example fix
// before
String id = request.getParameter("id"); // may be null
byte[] data = cache.get(id); // throws if id is null
// after
String id = request.getParameter("id");
byte[] data = (id != null) ? cache.get(id) : null; Defensive patterns
Strategy: validation
Validate before calling
public <V> V getFromCache(RRCache<String,V> cache, String key) {
if (key == null) return null; // or handle absence explicitly
return cache.get(key);
} Type guard
// Optional-based guard to make null explicit Optional<String> safeKey = Optional.ofNullable(rawKey); V result = safeKey.map(cache::get).orElse(null);
Try / catch
try {
return cache.get(key);
} catch (IllegalArgumentException e) {
if (key == null) return null; // graceful absent handling
throw e;
} Prevention
- Null-check keys before any cache interaction
- Use Optional in method signatures to signal nullable keys
- Filter null keys at the data-ingestion boundary
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Key and value must not be null
- Random must not be null
- Listener must not be null
- Eviction strategy must not be null
- Input must contain only '0' and '1'.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/d7e67a85ff22da41.
Report an issue: GitHub.