title: Null Pointer Exception When Autowiring Class
body: <p>I am building a REST API using Spring Boot with Shiro for authentication. I am getting a NullPointerException when I try to Autowire a repository in my realm code. Autowiring the repository elsewhere does not produce this error.</p>
  
  <p>Here is my authorizing realm:</p>
  
  <pre><code>@Component
  public class CCDAuthorizingRealm extends AuthorizingRealm {
  
      @Autowired
      private UserRepository userRepository;
  
      public CCDAuthorizingRealm() { }
  
      @Override
      protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
          AuthenticationInfo authenticationInfo = null;
          UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
          String username = token.getUsername();
          User user = null;
          try {
              user = userRepository.findByUsername(username);
          } catch (Exception e) {
              e.printStackTrace(System.err);
          }
  
          if (user != null) {
              authenticationInfo = new SimpleAuthenticationInfo(
                      new SimplePrincipalCollection(username, username),
                      user.getPassword());
          }
  
          return authenticationInfo;
      }
      ....
  }
  </code></pre>
  
  <p>The exception occurs when I try to call the findByUsername() method.</p>
  
  <p>Here is the code for the repository that I am injecting.</p>
  
  <pre><code>@Transactional(readOnly=true)
  @RepositoryRestResource(collectionResourceRel="users", path="users")
  public interface UserRepository extends PagingAndSortingRepository&lt;User, Long&gt; {
      User findByUsername(@Param("user") String user);
      ....
  }
  </code></pre>
  
  <p>To test, I autowired the repository into my Application class and printed the results, and I got no errors. For completeness, here is the code to show that:</p>
  
  <pre><code>@SpringBootApplication
  public class Application implements CommandLineRunner {
      @Autowired
      public UserRepository userRepository;
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
      @Override
      public void run(String... strings) {
          System.out.println("\nServer up\n");
          User user = userRepository.findOne(new Long(1));
          System.out.println(user.getUsername());
      }
  }
  </code></pre>
  
  <p>Any help is greatly appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As far as I can tell this question is not a duplicate, and the answer to the other question does not resolve my problem. I am injecting the bean and informing the IoC container of the component, but the autowiring still results in a NPE.</p>
  
  <p>Using scopes does not help, because I only need the single service object. The Configurable tag is not necessary, because I am autowiring and do not need to create a new object. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is where I am calling CCDAuthorizingRealm. I'm new to using Shiro, but I'm pretty sure this is correct.</p>
  
  <pre><code>@RestController
  @RequestMapping(produces="application/json")
  public class AuthenticationController {
  
      CCDAuthorizingRealm realm = new CCDAuthorizingRealm();
  
      SecurityManager sm = new DefaultSecurityManager(realm);
  
      public AuthenticationController() { }
  
      /**
       * Authenticate user with supplied credentials
       *
       * @param login - credentials
       * @return success or failure message
       */
      @RequestMapping(value = "/login", method = RequestMethod.POST, consumes="application/json")
      public String login(@RequestBody LoginInfo login) {
          String response;
          UsernamePasswordToken token = new UsernamePasswordToken(
                      login.getUsername(),
                      login.getPassword(),
                      false);
          try {
              SecurityUtils.setSecurityManager(sm);
              Subject currentUser = SecurityUtils.getSubject();
              if (currentUser.isAuthenticated()) {
                  currentUser.logout();
              }
              currentUser.login(token);
              response = token.toString();
          } catch (AuthenticationException e) {
              response = "Error: Incorrect username or password";
          }
  
          return response;
      }
  }
  </code></pre>
  
  <p>I currently try creating a new instance of my realm in the controller. I do this because when I try:</p>
  
  <pre><code>@Autowired
  CCDAuthorizingRealm realm;
  SecurityManager sm = new DefaultSecurityManager(realm);
  </code></pre>
  
  <p>I would get a runtime error when initializing the Security Manager because it would complain about realm being null. I don't understand why realm is null, however, since it is annotated as a component. Perhaps this is the root of my problem.</p>