title: Autowired component null pointer exception when calling method from another class
body: <p>I have a class with an Autowired component and a method. If I directly call the method, it works fine. But if the call to the method is from another class, then I got a java.lang.NullPointerException error in the line where the Autowired component is used. The Autowired component is an interface component that acts as a proxy. I have tried different annotations for both the interface component and the autowired component, but still getting the error.</p>
  
  <p>I don't get why the Autowired component is not null if the method is called directly but it's null if called from another class.</p>
  
  <p>This is the interface component</p>
  
  <pre><code>@FeignClient(name = "authentication-server", url = "localhost:8010")
  public interface AuthenticationProxy {
  
      @GetMapping("/headers")
      public HttpEntity&lt;String&gt; retrieveHeaders();
  
      @GetMapping("/auth-token")
      public AuthorizationTokenBean retrieveToken();
  </code></pre>
  
  <p>This is the class using the Autowired component</p>
  
  <pre><code>@RestController
  public class UserController {
  
      @Autowired
      private AuthenticationProxy authenticationProxy;
  
      @PostMapping("/user/create")
      public UserResponseBean createUser(ValuesBean userValues) {
  
          UserCreateRequestBean bodyBean = new UserCreateRequestBean();
          ValuesBean valuesBean = new ValuesBean();
  
          bodyBean.setValues(userValues); 
  
          // This line triggers the null pointer error 
          // (only if method called from another class)
          String token = authenticationProxy
              .retrieveToken()
              .getAuthorizationToken();
  
          HttpHeaders headers = new HttpHeaders();
          headers.add("Authorization", token);
          headers.add("Content-type", "application/json");
          headers.add("accept", "application/json");
  
          HttpEntity&lt;Object&gt; requestEntity =
                  new HttpEntity&lt;&gt;(bodyBean, headers);
  
          ResponseEntity&lt;String&gt; responseEntity = new RestTemplate().exchange(
                  "https://api.acme.com/user/create",
                  HttpMethod.POST,
                  requestEntity,
                  String.class
          );
  
          String output = responseEntity.getBody();
          Gson gson = new Gson();
          return gson.fromJson(output,UserResponseBean.class);
  
      }
  }
  </code></pre>
  
  <p>This is the class from where the method is called</p>
  
  <pre><code>@RestController
  public class TestController {
  
      @GetMapping("/test/user/create")
      public void testUserCreate() {
  
          ValuesBean valuesBean = new ValuesBean();
          valuesBean.setDate_of_birth("1917-05-16");
          valuesBean.setFirst_name("Juan");
          valuesBean.setLast_name("Rulfo");
          valuesBean.setGender("Male");
          valuesBean.setOccupation("Escritor");
  
      UserController testUser = new USerController();
      testUSer.createUser(valuesBean);
  
      }
  }
  </code></pre>