title: Spring Boot @Autowired object - Nullpointer Exception
body: <p>I am developing a spring boot application to send sms notification. This is my class for the purpose.</p>
  
  <pre><code>package org.otp.services;
  
  import org.otp.Configurations;
  import com.mashape.unirest.http.HttpResponse;
  import org.springframework.stereotype.Component;
  
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.context.annotation.PropertySources;
  
  @Component
  public class SmsService
  {
      private static final Logger LOG = LoggerFactory.getLogger(SmsService.class);
  
      public String send(String mobile, String msg)
      {
          //Code 
      }
  }
  </code></pre>
  
  <p>And this is the class which uses the above class for sending notification.</p>
  
  <pre><code>package org.otp.controllers;
  
  import org.otp.Constants;
  import org.otp.services.EmailService;
  import org.otp.services.SmsService;
  import org.otp.dto.MessageRequest;
  import org.otp.dto.MessageResponse;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.web.bind.annotation.RequestBody;
  
  @Component
  public class MessageController {
  
      private static final Logger LOG = LoggerFactory.getLogger(MessageController.class);
  
      @Autowired
      SmsService smsService;
  
      public void sendMessageToAlert(@RequestBody MessageRequest messageRequest)
      {
          String smsStatus = "FAIL";
          MessageResponse messageResponse = new MessageResponse();
  
           //1. Nullpointer
          smsStatus = smsService.send(messageRequest.getMobileNo(),messageRequest.getMessage());
  
      }
  }
  </code></pre>
  
  <p>Main Class</p>
  
  <pre><code>package org.otp;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.ApplicationArguments;
  import org.springframework.boot.ApplicationRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.scheduling.annotation.EnableAsync;
  
  @SpringBootApplication
  @EnableAsync
  public class OtpServiceApplication implements ApplicationRunner
  {
      public static void main(String[] args) {
          SpringApplication.run(OtpServiceApplication.class, args);
      }
  }
  </code></pre>
  
  <p>Problem is, I get a <strong>nullpointer exception</strong> in the (1) stating that my <strong>SmsService</strong> object is null. And my main class is in package <code>org.otp</code> so the two classes here falls under sub package so no need of component scan.</p>
  
  <p>Therefore I am confused what to do to solve this. I have tried many answers here like adding a <code>@Component</code> annotation and <code>@ComponentScan</code> in main class but nothing works. Could someone please point out my mistake here.</p>
  
  <p>Thanks in advance.</p>