title: Getting nullpointer on a spring managed class when trying to autowire
body: <p>I am creating a spring boot application to listen from a Rabbit queue and to forward the message to a Kafka stream.
  I am trying to <code>@Autowire</code> a Kafka configuring class but I am getting <code>NullPointerException</code>, more surprisingly when I list down all the spring managed beans then it gives me the object name of the class I am trying to <code>@Autowire</code>. I am not sure where I am missing.</p>
  <p><strong>Starting class:</strong></p>
  <pre><code>@SpringBootApplication
  @ComponentScan(basePackages = {&quot;com.example.demo.service&quot;,&quot;com.example.demo.configuration&quot;})
  public class RabbitConsumerApplication {
      
      private static ApplicationContext applicationContext;
  
      public static void main(String[] args) {
          applicationContext = SpringApplication.run(RabbitConsumerApplication.class, args);
          displayAllBeans();
      }
      
      public static void displayAllBeans() {
          String[] allBeanNames = applicationContext.getBeanDefinitionNames();
          for(String beanName : allBeanNames) {
              System.out.println(beanName);
          }
      }
      
  }
  </code></pre>
  <p><strong>KafkaProducerService class:</strong></p>
  <pre><code>package com.example.demo.service;
  
  @Component
  public class KafkaProducerService {
      
      
      @Autowired
      private KafkaTemplate&lt;?, String&gt; kafkaTemplate;
      
      
      public void getMessageFromMQ(Message  message) throws InterruptedException {
          
          Map&lt;String, Object&gt; configProps = new HashMap&lt;&gt;();
            configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, &quot;localhost:9092&quot;);
            configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            DefaultKafkaProducerFactory&lt;Object, Object&gt; defaultKafkaProducerFactory = new DefaultKafkaProducerFactory&lt;&gt;(configProps);
            
            
          kafkaTemplate.send(&quot;testTopic&quot;,new String(message.getBody()));
          
      }
  
  
      @Override
      public String toString() {
          return &quot;KafkaProducerService [kafkaTemplate=&quot; + kafkaTemplate + &quot;]&quot;;
      }
      
      
  
  }
  </code></pre>
  <p><strong>KafkaProducerConfig class:</strong></p>
  <pre><code>package com.example.demo.configuration;
  
  @Configuration
  public class KafkaProducerConfig {
  
      
      @Bean
      public ProducerFactory&lt;String, String&gt; producerFactory() {
        Map&lt;String, Object&gt; configProps = new HashMap&lt;&gt;();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, &quot;localhost:9092&quot;);
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return new DefaultKafkaProducerFactory&lt;&gt;(configProps);
      }
      
      @Bean
      public KafkaTemplate&lt;String, String&gt; kafkaTemplate() {
        return new KafkaTemplate&lt;&gt;(producerFactory());
      }
  }
  </code></pre>
  <p><strong>RabbitListenerService class:</strong></p>
  <pre><code>package com.example.demo.service;
  
  public class RabbitListenerService implements MessageListener{
      
      @Autowired
      private KafkaProducerService kafkaProducerService;
      
      
      @Override
      public void onMessage(Message message) {
          System.out.println(&quot;message :&quot;+new String(message.getBody()));
          System.out.println(&quot;object:&quot;+kafkaProducerService.toString());  //19th line. getting nullpointer on this line   
      }
  
  }
  </code></pre>
  <p><strong>Stack trace:</strong></p>
  <pre><code>2021-02-20 13:55:06.750  WARN 5392 --- [tnerContainer-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.
  
  org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Listener threw exception
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:1746) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1692) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1562) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1539) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1530) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1474) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:967) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:913) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:83) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1288) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1194) ~[spring-rabbit-2.3.4.jar:2.3.4]
      at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
  Caused by: java.lang.NullPointerException: null
      at com.example.demo.service.RabbitListenerService.onMessage(RabbitListenerService.java:19) ~[classes/:na]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1688) ~[spring-rabbit-2.3.4.jar:2.3.4]
      ... 10 common frames omitted
  
  message :hey you
  message :hey you
  </code></pre>
  <p>I just want to <code>@Autowire</code> KafkaProducerService class into RabbitListenerService.</p>