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

  1. Do not call evictionStrategy() at all to keep the default ImmediateEvictionStrategy.
  2. Provide a concrete strategy or a no-op implementation: cache -> 0.
  3. 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

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


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/3a53a19f9f3fab1d. Report an issue: GitHub.