redis/jedis · error · IllegalArgumentException

Cache cannot be null!

Error message

Cache cannot be null!

What it means

CacheConnection's Builder requires a non-null Cache instance; the constructor validates it immediately and throws IllegalArgumentException otherwise. The cache is the core collaborator of a client-side-caching connection, so building one without it is a programming error rather than a runtime condition. Fail-fast here prevents a half-initialized connection that would silently drop invalidation events.

Solutions

  1. Create a cache before building: Cache cache = CacheFactory.getCache(new CacheConfig()) and pass it to CacheConnection.builder(cache).
  2. Null-check the Cache argument at your own factory/builder boundary before delegating to CacheConnection.builder(cache).
  3. If the cache comes from DI/configuration, ensure the provider is registered and the cache bean is actually instantiated, not null.

Example fix

// before
CacheConnection.Builder b = CacheConnection.builder(cacheMaybeNull); // NPE-ish IAE thrown
// after
Objects.requireNonNull(cacheMaybeNull, "cache must be initialized");
CacheConnection.Builder b = CacheConnection.builder(cacheMaybeNull);
Defensive patterns

Strategy: validation

Validate before calling

if (cache == null) throw new IllegalStateException("Cache must be built via CacheFactory.getCache(config) before CacheConnection.builder(cache)");
CacheConnection connection = CacheConnection.builder(cache);

Prevention

When it happens

Trigger: Calling CacheConnection.builder(null) or new CacheConnection.Builder(null); passing a Cache field/variable that was never initialized or was returned null by a factory (e.g. CacheFactory.getCache throwing upstream and a surrounding layer swallowing it and returning null).

Common situations: Constructing the connection from configuration where the cache property was optional and left unset; wiring code that obtains the Cache lazily and passes an uninitialized field; copying example code but omitting the cache build step.

Related errors


AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08). Data as JSON: /api/errors/16453c78357f7692. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/redis/clients/jedis/csc/CacheConnection.java:24

import redis.clients.jedis.CommandObject;
import redis.clients.jedis.Connection;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisSocketFactory;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.PushConsumerChain;
import redis.clients.jedis.RedisProtocol;
import redis.clients.jedis.annots.VisibleForTesting;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.util.RedisInputStream;

public class CacheConnection extends Connection {

  public static class Builder extends Connection.Builder {
    private Cache cache;

    private Builder(Cache cache) {
      if (cache == null) {
        throw new IllegalArgumentException("Cache cannot be null!");
      }
      this.cache = cache;
    }

    public Cache getCache() {
      return cache;
    }

    @Override
    public Builder socketFactory(JedisSocketFactory socketFactory) {
      super.socketFactory(socketFactory);
      return this;
    }

    @Override
    public Builder clientConfig(JedisClientConfig clientConfig) {
      super.clientConfig(clientConfig);
      return this;

View on GitHub (pinned to 6dac31d4c2)