TheAlgorithms/Java · error · IllegalArgumentException
Key and value must not be null
Error message
Key and value must not be null
What it means
RRCache.put() rejects null keys and null values with IllegalArgumentException before acquiring the lock. The cache does not support null values because null is not distinguishable from an absent entry in its internal structures, and null keys break key-tracking consistency.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/RRCache.java:165
put(key, value, defaultTTL);
}
/**
* Adds a key-value pair to the cache with a specified time-to-live (TTL).
*
* <p>If the key already exists, its value is updated and its TTL is reset. If the key
* does not exist and the cache is full, a random entry is evicted to make space.
* Expired entries are also cleaned up prior to any eviction. The eviction listener
* is notified when an entry gets evicted.
*
* @param key the key to associate with the cached value; must not be {@code null}
* @param value the value to be cached; must not be {@code null}
* @param ttlMillis the time-to-live for this entry in milliseconds; must be >= 0
* @throws IllegalArgumentException if {@code key} or {@code value} is {@code null}, or if {@code ttlMillis} is negative
*/
public void put(K key, V value, long ttlMillis) {
if (key == null || value == null) {
throw new IllegalArgumentException("Key and value must not be null");
}
if (ttlMillis < 0) {
throw new IllegalArgumentException("TTL must be >= 0");
}
lock.lock();
try {
if (cache.containsKey(key)) {
cache.put(key, new CacheEntry<>(value, ttlMillis));
return;
}
evictExpired();
if (cache.size() >= capacity) {
int idx = random.nextInt(keys.size());
K evictKey = keys.remove(idx);
CacheEntry<V> evictVal = cache.remove(evictKey);View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate both key and value are non-null before calling put()
- If null values are semantically valid, wrap them in Optional or use a sentinel object
- Skip caching entirely when the value is null rather than storing it
Example fix
// before
cache.put(userId, userProfile); // throws if userProfile is null
// after
if (userId != null && userProfile != null) {
cache.put(userId, userProfile);
} Defensive patterns
Strategy: validation
Validate before calling
public void safePut(RRCache<String,String> cache, String key, String val) {
if (key == null || val == null) return;
cache.put(key, val);
} Type guard
Objects.requireNonNull(key, "key"); Objects.requireNonNull(value, "value"); cache.put(key, value);
Try / catch
try {
cache.put(key, value);
} catch (IllegalArgumentException e) {
if (key == null || value == null) {
logger.debug("Skipped caching null key or value");
} else throw e;
} Prevention
- Validate key and value are non-null before put()
- Use Objects.requireNonNull for fast-fail at call sites
- Skip caching when the value is null rather than storing it
When it happens
Trigger: Calling cache.put(null, value), cache.put(key, null), or cache.put(null, null). Also triggered via the two-argument put(key, value) which delegates to put(key, value, defaultTTL).
Common situations: Caching a computation result that can be null on edge cases (e.g., a DB query with no match). Key produced by a deserializer or mapper that yields null for missing fields.
Related errors
- Key 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/9728869e5329c4f6.
Report an issue: GitHub.