title: @Autowired service inside of @Component returns null pointer exception
body: <p>My Spring Web App returns <code>java.lang.NullPointerException</code> when I try to use a service which is Autowired inside a component. </p>
  
  <p>I tried solutions <a href="https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null">here</a> but it didn't work for me since I am already using <code>@component</code>.</p>
  
  <p>Here is my full exception:</p>
  
  <pre><code>java.lang.NullPointerException
      at ir.yasharne.rpi.websocket.MyStompSessionHandler.handleFrame(MyStompSessionHandler.java:81)
      at org.springframework.messaging.simp.stomp.DefaultStompSession.invokeHandler(DefaultStompSession.java:448)
      at org.springframework.messaging.simp.stomp.DefaultStompSession.handleMessage(DefaultStompSession.java:399)
      at org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter.handleMessage(WebSocketStompClient.java:342)
      at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleMessageFrame(AbstractClientSockJsSession.java:271)
      at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleFrame(AbstractClientSockJsSession.java:213)
      at org.springframework.web.socket.sockjs.client.WebSocketTransport$ClientSockJsWebSocketHandler.handleTextMessage(WebSocketTransport.java:162)
      at org.springframework.web.socket.handler.AbstractWebSocketHandler.handleMessage(AbstractWebSocketHandler.java:43)
      at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:110)
      at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$000(StandardWebSocketHandlerAdapter.java:42)
      at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:81)
      at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:78)
      at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:395)
      at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:495)
      at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:294)
      at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133)
      at org.apache.tomcat.websocket.WsFrameClient.processSocketRead(WsFrameClient.java:95)
      at org.apache.tomcat.websocket.WsFrameClient.resumeProcessing(WsFrameClient.java:209)
      at org.apache.tomcat.websocket.WsFrameClient.access$300(WsFrameClient.java:31)
      at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.doResumeProcessing(WsFrameClient.java:186)
      at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:163)
      at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:148)
      at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
      at sun.nio.ch.Invoker$2.run(Invoker.java:218)
      at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>line 81 is <code>moduleService.saveModule(module);</code> in <code>MyStompSessionHandler.java</code>:</p>
  
  <pre><code>@Component
  public class MyStompSessionHandler extends StompSessionHandlerAdapter {
  
      @Autowired
      private ModuleService moduleService;
  
      @Override
      public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
          session.subscribe("/topic/messages", this);
      }
  
      @Override
      public Type getPayloadType(StompHeaders headers) {
          return ServerMessage.class;
      }
  
      @Override
      public void handleFrame(StompHeaders headers, Object payload) {
          ServerMessage message = (ServerMessage) payload;
          if (message.getTopic().equals("newModule")){
              ObjectMapper mapper = new ObjectMapper();
              try {
                  Map&lt;String, Object&gt; readValue = mapper.readValue(message.getMessage(), Map.class);
                  int id = (int) readValue.get("id");
                  boolean programmable = (boolean) readValue.get("programmable");
                  float value = Float.parseFloat(readValue.get("value").toString());
                  Module module = new Module();
                  module.setId(id);
                  module.setProgrammable(programmable);
                  module.setValue(value);
                  moduleService.saveModule(module);
              } catch (IOException e) {
                  System.out.println("HandlerFrame: ");
                  e.printStackTrace();
              }
          }
      }
  
      @Override
      public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
          exception.printStackTrace();
      }
  }
  </code></pre>
  
  <p>and my <code>WebAppConfiguration.java</code>:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = "ir.yasharne.rpi")
  public class WebAppConfiguration extends WebMvcConfigurerAdapter {
  
      @Bean
      public ViewResolver viewResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setViewClass(JstlView.class);
          viewResolver.setPrefix("/WEB-INF/views/");
          viewResolver.setSuffix(".jsp");
          return viewResolver;
      }
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registery){
          registery.addResourceHandler("/static/**").addResourceLocations("/static/");
      }
  }
  </code></pre>
  
  <p><code>ModuleServiceImpl.java</code>: </p>
  
  <pre><code>@Service("moduleService")
  @Transactional
  public class ModuleServiceImpl implements ModuleService {
  
      @Autowired
      private ModuleDao moduleDao;
  
      @Override
      public void saveModule(Module module) {
          moduleDao.saveModule(module);
      }
  }
  </code></pre>
  
  <p>How can I fix it?</p>
  
  <p>BTW, I can Autowire and use <code>moduleService</code> inside of controller and use it with no problem</p>