TheAlgorithms/Java · error · IllegalArgumentException
Eviction strategy must not be null
Error message
Eviction strategy must not be null
What it means
Builder.evictionStrategy() rejects null because it needs a concrete EvictionStrategy to decide when to clean up expired entries. If you never call this method, the builder defaults to PeriodicEvictionStrategy(100). Passing null is always a programming error.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/RRCache.java:499
/**
* Builds and returns a new {@link RRCache} instance with the configured parameters.
*
* @return a fully configured {@code RRCache} instance
*/
public RRCache<K, V> build() {
return new RRCache<>(this);
}
/**
* Sets the eviction strategy used to determine when to clean up expired entries.
*
* @param strategy an {@link EvictionStrategy} implementation; must not be {@code null}
* @return this builder instance
* @throws IllegalArgumentException if {@code strategy} is {@code null}
*/
public Builder<K, V> evictionStrategy(EvictionStrategy<K, V> strategy) {
if (strategy == null) {
throw new IllegalArgumentException("Eviction strategy must not be null");
}
this.evictionStrategy = strategy;
return this;
}
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Do not call evictionStrategy() if the default PeriodicEvictionStrategy(100) is acceptable
- Use NoEvictionStrategy for per-access cleanup, or provide a custom implementation
- Guard the call so it only fires when the strategy is non-null
Example fix
// before EvictionStrategy<String,String> strat = strategyFactory.get(); // may be null new RRCache.Builder<String,String>(100).evictionStrategy(strat).build(); // after RRCache.Builder<String,String> b = new RRCache.Builder<>(100); EvictionStrategy<String,String> strat = strategyFactory.get(); if (strat != null) b.evictionStrategy(strat); b.build();
Defensive patterns
Strategy: validation
Validate before calling
// Omit evictionStrategy() to use the default PeriodicEvictionStrategy(100).
// Only call with a concrete implementation:
if (strategy != null) {
builder.evictionStrategy(strategy);
} else {
builder.evictionStrategy(new RRCache.NoEvictionStrategy<>());
} Type guard
if (strategy instanceof RRCache.EvictionStrategy) {
builder.evictionStrategy((RRCache.EvictionStrategy<K,V>) strategy);
} Try / catch
try {
builder.evictionStrategy(strategy);
} catch (IllegalArgumentException e) {
builder.evictionStrategy(new RRCache.PeriodicEvictionStrategy<>(100));
} Prevention
- Do not call evictionStrategy() unless you have a concrete implementation
- Use NoEvictionStrategy for per-access cleanup or PeriodicEvictionStrategy for batched cleanup
- Guard the call with a null check
When it happens
Trigger: Calling builder.evictionStrategy(null), or passing a strategy variable that was never assigned.
Common situations: Intending to use the default strategy but calling the method with null instead of omitting it. Passing a strategy from a factory that can return null on misconfiguration.
Related errors
- Random must not be null
- Listener must not be null
- Key must not be null
- Key and value must not be null
- Interval must be > 0
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/7c31096ee61f9182.
Report an issue: GitHub.