{"record":{"id":"7c31096ee61f9182","repo":"TheAlgorithms/Java","slug":"eviction-strategy-must-not-be-null-7c3109","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/RRCache.java","lineNumber":499,"sourceCode":"        /**\n         * Builds and returns a new {@link RRCache} instance with the configured parameters.\n         *\n         * @return a fully configured {@code RRCache} instance\n         */\n        public RRCache<K, V> build() {\n            return new RRCache<>(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":481,"sourceCodeEnd":506,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/RRCache.java#L481-L506","documentation":"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.","triggerScenarios":"Calling builder.evictionStrategy(null), or passing a strategy variable that was never assigned.","commonSituations":"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.","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"],"exampleFix":"// before\nEvictionStrategy<String,String> strat = strategyFactory.get(); // may be null\nnew RRCache.Builder<String,String>(100).evictionStrategy(strat).build();\n\n// after\nRRCache.Builder<String,String> b = new RRCache.Builder<>(100);\nEvictionStrategy<String,String> strat = strategyFactory.get();\nif (strat != null) b.evictionStrategy(strat);\nb.build();","handlingStrategy":"validation","validationCode":"// Omit evictionStrategy() to use the default PeriodicEvictionStrategy(100).\n// Only call with a concrete implementation:\nif (strategy != null) {\n    builder.evictionStrategy(strategy);\n} else {\n    builder.evictionStrategy(new RRCache.NoEvictionStrategy<>());\n}","typeGuard":"if (strategy instanceof RRCache.EvictionStrategy) {\n    builder.evictionStrategy((RRCache.EvictionStrategy<K,V>) strategy);\n}","tryCatchPattern":"try {\n    builder.evictionStrategy(strategy);\n} catch (IllegalArgumentException e) {\n    builder.evictionStrategy(new RRCache.PeriodicEvictionStrategy<>(100));\n}","preventionTips":["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"],"tags":["rr-cache","null-safety","validation","builder","eviction-strategy"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}