I always run the method that accesses what is in another class with @Autowired it becomes null, follow the code below:
@Component
public class TokenAuthenticationService {
@Autowired
UserRoleServiceSecurity service;
...
void addAuthentication(HttpServletResponse response, String username) throws IOException {
UserRole us = service.buscarPorUsername(username); //Aqui fica nulo e solta um java.lang.NullPointerException: null
System.out.println(us.getRole());
The UserRoleServiceSecurity.java class:
@Service
public class UserRoleServiceSecurity {
@Autowired
UsuarioRepository repository;
@Autowired
UsuarioRoleRepository usRepository;
public UserRole buscarPorUsername(String username) {
return usRepository.buscarUserUsername(username);
}
}