TheAlgorithms/Java · error · IllegalArgumentException
Eviction strategy must not be null
Error message
Eviction strategy must not be null
What it means
Thrown by FIFOCache.Builder.evictionStrategy(EvictionStrategy) when the strategy is null. The strategy is invoked on every get/put via onAccess; a null would NPE on every cache operation. The builder already defaults to ImmediateEvictionStrategy, so this guard only fires if a caller explicitly sets null.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java:543
/**
* Builds and returns a new {@link FIFOCache} instance with the configured parameters.
*
* @return a fully configured {@code FIFOCache} instance
*/
public FIFOCache<K, V> build() {
return new FIFOCache<>(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() at all to keep the default ImmediateEvictionStrategy.
- Provide a concrete strategy or a no-op implementation: cache -> 0.
- Ensure your strategy-selection helper never returns null.
Example fix
// before builder.evictionStrategy(selectStrategy(mode)); // after EvictionStrategy<K,V> s = selectStrategy(mode); if (s != null) builder.evictionStrategy(s);
Defensive patterns
Strategy: validation
Validate before calling
EvictionStrategy<K, V> s = selectStrategy(mode); if (s != null) builder.evictionStrategy(s);
Type guard
static <K, V> boolean isUsableStrategy(EvictionStrategy<K, V> s) {
return s != null;
} Prevention
- To keep the default, simply do not call evictionStrategy().
- Ensure strategy-selection helpers never return null; return a no-op (cache -> 0) instead.
- Cover all enum branches when selecting a strategy.
When it happens
Trigger: builder.evictionStrategy(null); conditional wiring where the chosen strategy is null in some branch; DI misconfiguration.
Common situations: Selecting strategy by enum where a case returns null; tests that reset the strategy; refactoring that left a branch returning null.
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
- Key cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/3a53a19f9f3fab1d.
Report an issue: GitHub.