{"record":{"id":"16d2affaf5e1f6f8","repo":"TheAlgorithms/Java","slug":"random-must-not-be-null","errorCode":null,"errorMessage":"Random must not be null","messagePattern":"Random must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/caches/RRCache.java","lineNumber":460,"sourceCode":"         */\n        public Builder<K, V> defaultTTL(long ttlMillis) {\n            if (ttlMillis < 0) {\n                throw new IllegalArgumentException(\"Default TTL must be >= 0\");\n            }\n            this.defaultTTL = ttlMillis;\n            return this;\n        }\n\n        /**\n         * Sets the {@link Random} instance to be used for random eviction selection.\n         *\n         * @param r a non-null {@code Random} instance\n         * @return this builder instance for chaining\n         * @throws IllegalArgumentException if {@code r} is {@code null}\n         */\n        public Builder<K, V> random(Random r) {\n            if (r == null) {\n                throw new IllegalArgumentException(\"Random must not be null\");\n            }\n            this.random = r;\n            return this;\n        }\n\n        /**\n         * Sets an eviction listener to be notified when entries are evicted from the cache.\n         *\n         * @param listener a {@link BiConsumer} that accepts evicted keys and values; must not be {@code null}\n         * @return this builder instance for chaining\n         * @throws IllegalArgumentException if {@code listener} is {@code null}\n         */\n        public Builder<K, V> evictionListener(BiConsumer<K, V> listener) {\n            if (listener == null) {\n                throw new IllegalArgumentException(\"Listener must not be null\");\n            }\n            this.evictionListener = listener;\n            return this;","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/caches/RRCache.java#L442-L478","documentation":"Builder.random() rejects null because a Random instance is required for eviction selection. If this method is never called, the cache creates its own default Random, so calling it with null is always a programming error.","triggerScenarios":"Calling builder.random(null), or passing a Random field that has not been initialized.","commonSituations":"Passing an injected dependency that was not wired. Intending to reset to the default but accidentally calling the method with null instead of omitting it.","solutions":["Do not call random() at all if you want the default Random","Pass a valid Random or a subclass such as SecureRandom or a seeded Random for reproducibility"],"exampleFix":"// before\nnew RRCache.Builder<String,String>(100).random(maybeNull).build();\n\n// after — omit the call to use the default, or pass a valid instance\nnew RRCache.Builder<String,String>(100)\n    .random(maybeNull != null ? maybeNull : new Random())\n    .build();","handlingStrategy":"validation","validationCode":"// Simply omit the random() call to use the default.\n// Only call it with a guaranteed non-null instance:\nif (customRandom != null) {\n    builder.random(customRandom);\n}","typeGuard":"if (r instanceof Random) {\n    builder.random((Random) r);\n} // else: skip, use default","tryCatchPattern":"try {\n    builder.random(r);\n} catch (IllegalArgumentException e) {\n    // r was null — use default instead\n}","preventionTips":["Do not call random() unless you have a specific Random to inject","Use SecureRandom for security-sensitive contexts","Seed Random for reproducible eviction in tests"],"tags":["rr-cache","null-safety","validation","builder"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}