title: Spring Redis Null Pointer Exception on redisTemplate
body: <p>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:</p>
  
  <p><strong>RedisConfig.java</strong></p>
  
  <pre><code>@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 &lt;K, V&gt; RedisTemplate&lt;String, V&gt; getRedisTemplate() {
      JdkSerializationRedisSerializer jackson2JsonRedisSerializer = new JdkSerializationRedisSerializer();
      RedisTemplate&lt;String, V&gt; redisTemplate = new RedisTemplate&lt;String, V&gt;();
      redisTemplate.setConnectionFactory(jedisConnectionFactory());
      redisTemplate.setKeySerializer(new StringRedisSerializer());
      redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
      redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
      redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
      redisTemplate.setHashKeySerializer(new StringRedisSerializer());
      return redisTemplate;
    }
  
  }
  </code></pre>
  
  <p><strong>CacheRepository.java</strong></p>
  
  <pre><code>public interface CacheRepository&lt;T, V&gt; {
      V get(Object key);
  
      void add(Object redisKey, Object key, T value);
  
      void add(T key, V value);
  
      void expire(Object key);
  }
  </code></pre>
  
  <p><strong>CacheRepositoryImpl.java</strong></p>
  
  <pre><code>@Component
  public class CacheRepositoryImpl&lt;T, V&gt; implements CacheRepository&lt;T, V&gt; {
  
      @Autowired
      protected RedisTemplate&lt;String, V&gt; 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) {
      }
  }
  </code></pre>
  
  <p><strong>VendorInformationCacheRepository.java</strong></p>
  
  <pre><code>public class VendorInformationCacheRepository extends CacheRepositoryImpl&lt;String, HashMap&lt;String, List&lt;Vehicles&gt;&gt;&gt; {
  
  private static final String KEY = "Vendors";
  
  @Override
  public void add(String key, HashMap&lt;String, List&lt;Vehicles&gt;&gt; value){
      System.out.println(redisTemplate);                 // this prints null
      redisTemplate.opsForHash().put(KEY, key, value);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>BaseController.java</strong></p>
  
  <pre><code>VendorInformationCacheRepository vicr = new VendorInformationCacheRepository();
  vicr.add("testKey",  myNewList);
  </code></pre>
  
  <p>In <strong>VendorInformationCacheRepository.java</strong> <code>redisTemplate.opsForHash().put(KEY, key, value);</code> gives null pointer exception. 
  This is the stack trace of the error: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Any help would be highly appreciated. Thanks in advance. Cheers</p>