{"record":{"id":"16453c78357f7692","repo":"redis/jedis","slug":"cache-cannot-be-null","errorCode":null,"errorMessage":"Cache cannot be null!","messagePattern":"Cache cannot be null!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/csc/CacheConnection.java","lineNumber":24,"sourceCode":"import redis.clients.jedis.CommandObject;\nimport redis.clients.jedis.Connection;\nimport redis.clients.jedis.JedisClientConfig;\nimport redis.clients.jedis.JedisSocketFactory;\nimport redis.clients.jedis.Protocol;\nimport redis.clients.jedis.PushConsumerChain;\nimport redis.clients.jedis.RedisProtocol;\nimport redis.clients.jedis.annots.VisibleForTesting;\nimport redis.clients.jedis.exceptions.JedisException;\nimport redis.clients.jedis.util.RedisInputStream;\n\npublic class CacheConnection extends Connection {\n\n  public static class Builder extends Connection.Builder {\n    private Cache cache;\n\n    private Builder(Cache cache) {\n      if (cache == null) {\n        throw new IllegalArgumentException(\"Cache cannot be null!\");\n      }\n      this.cache = cache;\n    }\n\n    public Cache getCache() {\n      return cache;\n    }\n\n    @Override\n    public Builder socketFactory(JedisSocketFactory socketFactory) {\n      super.socketFactory(socketFactory);\n      return this;\n    }\n\n    @Override\n    public Builder clientConfig(JedisClientConfig clientConfig) {\n      super.clientConfig(clientConfig);\n      return this;","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/csc/CacheConnection.java#L6-L42","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Create a cache before building: Cache cache = CacheFactory.getCache(new CacheConfig()) and pass it to CacheConnection.builder(cache).","Null-check the Cache argument at your own factory/builder boundary before delegating to CacheConnection.builder(cache).","If the cache comes from DI/configuration, ensure the provider is registered and the cache bean is actually instantiated, not null."],"exampleFix":"// before\nCacheConnection.Builder b = CacheConnection.builder(cacheMaybeNull); // NPE-ish IAE thrown\n// after\nObjects.requireNonNull(cacheMaybeNull, \"cache must be initialized\");\nCacheConnection.Builder b = CacheConnection.builder(cacheMaybeNull);","handlingStrategy":"validation","validationCode":"if (cache == null) throw new IllegalStateException(\"Cache must be built via CacheFactory.getCache(config) before CacheConnection.builder(cache)\");\nCacheConnection connection = CacheConnection.builder(cache);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always create the Cache via CacheFactory.getCache(new CacheConfig()) immediately before CacheConnection.builder(cache)","Never swallow exceptions from cache factories — a swallowed failure often leaves the Cache variable null","Add an assert/Objects.requireNonNull(cache) check in your own connection factory"],"tags":["null-argument","illegal-argument","client-side-caching"],"backgroundTag":"null-argument","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}