TheAlgorithms/Java · error · IllegalArgumentException
Random must not be null
Error message
Random must not be null
What it means
Builder.random() rejects null because a Random instance is required for eviction selection. If this method is never called, the cache creates its own default Random, so calling it with null is always a programming error.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/RRCache.java:460
*/
public Builder<K, V> defaultTTL(long ttlMillis) {
if (ttlMillis < 0) {
throw new IllegalArgumentException("Default TTL must be >= 0");
}
this.defaultTTL = ttlMillis;
return this;
}
/**
* Sets the {@link Random} instance to be used for random eviction selection.
*
* @param r a non-null {@code Random} instance
* @return this builder instance for chaining
* @throws IllegalArgumentException if {@code r} is {@code null}
*/
public Builder<K, V> random(Random r) {
if (r == null) {
throw new IllegalArgumentException("Random must not be null");
}
this.random = r;
return this;
}
/**
* Sets an eviction listener to be notified when entries are evicted from the cache.
*
* @param listener a {@link BiConsumer} that accepts evicted keys and values; must not be {@code null}
* @return this builder instance for chaining
* @throws IllegalArgumentException if {@code listener} is {@code null}
*/
public Builder<K, V> evictionListener(BiConsumer<K, V> listener) {
if (listener == null) {
throw new IllegalArgumentException("Listener must not be null");
}
this.evictionListener = listener;
return this;View on GitHub (pinned to fdfb9a395b)
Solutions
- Do not call random() at all if you want the default Random
- Pass a valid Random or a subclass such as SecureRandom or a seeded Random for reproducibility
Example fix
// before
new RRCache.Builder<String,String>(100).random(maybeNull).build();
// after — omit the call to use the default, or pass a valid instance
new RRCache.Builder<String,String>(100)
.random(maybeNull != null ? maybeNull : new Random())
.build(); Defensive patterns
Strategy: validation
Validate before calling
// Simply omit the random() call to use the default.
// Only call it with a guaranteed non-null instance:
if (customRandom != null) {
builder.random(customRandom);
} Type guard
if (r instanceof Random) {
builder.random((Random) r);
} // else: skip, use default Try / catch
try {
builder.random(r);
} catch (IllegalArgumentException e) {
// r was null — use default instead
} Prevention
- Do not call random() unless you have a specific Random to inject
- Use SecureRandom for security-sensitive contexts
- Seed Random for reproducible eviction in tests
When it happens
Trigger: Calling builder.random(null), or passing a Random field that has not been initialized.
Common situations: Passing an injected dependency that was not wired. Intending to reset to the default but accidentally calling the method with null instead of omitting it.
Related errors
- Listener must not be null
- Eviction strategy must not be null
- Key must not be null
- Key and value must not be null
- Capacity must be > 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/16d2affaf5e1f6f8.
Report an issue: GitHub.