{"record":{"id":"3a53a19f9f3fab1d","repo":"TheAlgorithms/Java","slug":"eviction-strategy-must-not-be-null","errorCode":null,"errorMessage":"Eviction strategy must not be null","messagePattern":"Eviction strategy must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java","lineNumber":543,"sourceCode":"        /**\n         * Builds and returns a new {@link FIFOCache} instance with the configured parameters.\n         *\n         * @return a fully configured {@code FIFOCache} instance\n         */\n        public FIFOCache<K, V> build() {\n            return new FIFOCache<>(this);\n        }\n\n        /**\n         * Sets the eviction strategy used to determine when to clean up expired entries.\n         *\n         * @param strategy an {@link EvictionStrategy} implementation; must not be {@code null}\n         * @return this builder instance\n         * @throws IllegalArgumentException if {@code strategy} is {@code null}\n         */\n        public Builder<K, V> evictionStrategy(EvictionStrategy<K, V> strategy) {\n            if (strategy == null) {\n                throw new IllegalArgumentException(\"Eviction strategy must not be null\");\n            }\n            this.evictionStrategy = strategy;\n            return this;\n        }\n    }\n}\n","sourceCodeStart":525,"sourceCodeEnd":550,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/FIFOCache.java#L525-L550","documentation":"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.","triggerScenarios":"builder.evictionStrategy(null); conditional wiring where the chosen strategy is null in some branch; DI misconfiguration.","commonSituations":"Selecting strategy by enum where a case returns null; tests that reset the strategy; refactoring that left a branch returning null.","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."],"exampleFix":"// before\nbuilder.evictionStrategy(selectStrategy(mode));\n// after\nEvictionStrategy<K,V> s = selectStrategy(mode);\nif (s != null) builder.evictionStrategy(s);","handlingStrategy":"validation","validationCode":"EvictionStrategy<K, V> s = selectStrategy(mode);\nif (s != null) builder.evictionStrategy(s);","typeGuard":"static <K, V> boolean isUsableStrategy(EvictionStrategy<K, V> s) {\n    return s != null;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["java","fifo-cache","builder","null-check","eviction-strategy","argument-validation","cache"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}