TheAlgorithms/Java · error · IllegalArgumentException

Listener must not be null

Error message

Listener must not be null

What it means

Builder.evictionListener() rejects null because it expects a BiConsumer callback for eviction events. If no listener is needed, simply do not call this method — the cache handles a null listener field internally.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/caches/RRCache.java:475

         */
        public Builder<K, V> random(Random r) {
            if (r == null) {
                throw new IllegalArgumentException("Random must not be null");
            }
            this.random = r;
            return this;
        }

        /**
         * Sets an eviction listener to be notified when entries are evicted from the cache.
         *
         * @param listener a {@link BiConsumer} that accepts evicted keys and values; must not be {@code null}
         * @return this builder instance for chaining
         * @throws IllegalArgumentException if {@code listener} is {@code null}
         */
        public Builder<K, V> evictionListener(BiConsumer<K, V> listener) {
            if (listener == null) {
                throw new IllegalArgumentException("Listener must not be null");
            }
            this.evictionListener = listener;
            return this;
        }

        /**
         * Builds and returns a new {@link RRCache} instance with the configured parameters.
         *
         * @return a fully configured {@code RRCache} instance
         */
        public RRCache<K, V> build() {
            return new RRCache<>(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}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Do not call evictionListener() if you do not need eviction callbacks
  2. Only call the method inside a conditional that guarantees a non-null listener

Example fix

// before
BiConsumer<String,String> listener = enableLogging ? loggingListener : null;
new RRCache.Builder<String,String>(100).evictionListener(listener).build();

// after
RRCache.Builder<String,String> b = new RRCache.Builder<>(100);
if (enableLogging) b.evictionListener(loggingListener);
b.build();
Defensive patterns

Strategy: validation

Validate before calling

// Only call evictionListener() when you have a real callback.
if (listener != null) {
    builder.evictionListener(listener);
}

Type guard

if (listener instanceof BiConsumer) {
    builder.evictionListener((BiConsumer<K,V>) listener);
}

Try / catch

try {
    builder.evictionListener(listener);
} catch (IllegalArgumentException e) {
    // listener was null — skip, no callbacks
}

Prevention

When it happens

Trigger: Calling builder.evictionListener(null), or passing a listener variable that was never assigned.

Common situations: Conditionally wiring a listener and passing null when the condition is false, instead of skipping the call. Passing an uninjected dependency.

Related errors


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