Search code examples
javaspringredisjedis

Spring Redis Null Pointer Exception on redisTemplate


Hi I am working with java-redis integration with jedis and I came up with this problem when trying to setup project. I am new to spring so there might be some issues with initial setup. This is the code that I am trying to work with:

RedisConfig.java

@Configuration
public class RedisConfig {

  @Bean
  public RedisConnectionFactory jedisConnectionFactory() {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(1000);

    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
    jedisConnectionFactory.setHostName(Properties.getInstance().getPropertyValueReader().getAWSRedisEndPoint());
    jedisConnectionFactory.setPort(6379);
    return jedisConnectionFactory;
  }

  @Bean
  public <K, V> RedisTemplate<String, V> getRedisTemplate() {
    JdkSerializationRedisSerializer jackson2JsonRedisSerializer = new JdkSerializationRedisSerializer();
    RedisTemplate<String, V> redisTemplate = new RedisTemplate<String, V>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
    redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    return redisTemplate;
  }

}

CacheRepository.java

public interface CacheRepository<T, V> {
    V get(Object key);

    void add(Object redisKey, Object key, T value);

    void add(T key, V value);

    void expire(Object key);
}

CacheRepositoryImpl.java

@Component
public class CacheRepositoryImpl<T, V> implements CacheRepository<T, V> {

    @Autowired
    protected RedisTemplate<String, V> redisTemplate;

    @Override
    public V get(Object key) {
    }

    @Override
    public void expire(Object key) {
    }

    @Override
    public void add(Object redisKey, Object key, T value) {
    }

    @Override
    public void add(T key, V value) {
    }
}

VendorInformationCacheRepository.java

public class VendorInformationCacheRepository extends CacheRepositoryImpl<String, HashMap<String, List<Vehicles>>> {

private static final String KEY = "Vendors";

@Override
public void add(String key, HashMap<String, List<Vehicles>> value){
    System.out.println(redisTemplate);                 // this prints null
    redisTemplate.opsForHash().put(KEY, key, value);
}

}

BaseController.java

VendorInformationCacheRepository vicr = new VendorInformationCacheRepository();
vicr.add("testKey",  myNewList);

In VendorInformationCacheRepository.java redisTemplate.opsForHash().put(KEY, key, value); gives null pointer exception. This is the stack trace of the error:

java.lang.NullPointerException

at com.moveinsync.cache.CacheRepositoryImpl.add(CacheRepositoryImpl.java:35)
at com.moveinsync.controllers.BaseController.redisTest(BaseController.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

Any help would be highly appreciated. Thanks in advance. Cheers


Solution

  • Figured out what I was doing wrong. Inside of my base controller I was initializing the object as :

    VendorInformationCacheRepository vicr = new VendorInformationCacheRepository();
    

    This would lead to losing all the autowiring done inside of the bean.All I had to do was call the method on the class as the class was already autowired.