Nullpointerexception when calling method return in another class

Asked

Viewed 177 times

0

I have the following method in the Logincontroller class

@RequestMapping(value = "/usuarioEntidade", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
public Resposta usuarioEntidade(@RequestBody EntidadesAdministradores usuarioEntidade) throws ServletException {
    EntidadesAdministradores entAdministradoresAutenticado = eaService.buscarUsuarioEntidade(usuarioEntidade.getUsuarioAdministrador());        
    usuarioEntidade = entAdministradoresAutenticado;
    Long us = usuarioEntidade.getEntidade().getIdEntidade();

    return new Resposta (us);
}

public class Resposta {
        public Long us;
        public Resposta(Long us) {
            this.us = us;
        }

        public Long getUs() {
            return us; 
        }
    }

I have debugged and the return of the method is coming what I want, a type id Long.

Passing the return of this method , us, for the Distritocontroller class, when I call the request /distritos have a mistake of java.lang.NullPointerException

Distritocontroller

 @Autowired
    DistritosService distritosService; 
    Resposta resp; 

    @RequestMapping(method = RequestMethod.GET, value = "/distritos", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Distritos>> buscarTodosDistritos(Long usuarioEntidade) throws ServletException { 
 usuarioEntidade = resp.getUs();
        Collection<Distritos> distritosBuscados = distritosService.buscarFiltro(usuarioEntidade);
        return new ResponseEntity<>(distritosBuscados, HttpStatus.OK);
    } 
  • The stacktrace of NullPointerException points to the line of Collection<Distritos> distritosBuscados = distritosService.buscarFiltro(usuarioEntidade);?

  • points to this line controller.DistritosController.buscarTodosDistritos(DistritosController.java:44) ~[classes/:na] that is usuarioEntidade = resp.getUs();

  • That is to say, resp is null. How does the class LoginController flame to DistritoController? Where the resp could be being set?

  • Useful reading: https://answall.com/q/63617/64969

1 answer

2


The exception is because your attribute resp apparently null.

You could return the logged in user id directly, as in the code below:

@RequestMapping(value = "/usuarioEntidade", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
public Long usuarioEntidade(@RequestBody EntidadesAdministradores usuarioEntidade) throws ServletException {
    EntidadesAdministradores entAdministradoresAutenticado = eaService.buscarUsuarioEntidade(usuarioEntidade.getUsuarioAdministrador());        
    usuarioEntidade = entAdministradoresAutenticado;
    return usuarioEntidade.getEntidade().getIdEntidade();
}

With this, you could save this user id logged in to your account, in a cookie or localStorage for example.

And then you could adapt the method buscarTodosDistritos() of your DistritoController to receive the user id from the URL, as recommended by the REST philosophy:

@RequestMapping(method = RequestMethod.GET, value = "/distritos/{usuario}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Distritos>> buscarTodosDistritos(@PathVariable Long usuario) throws ServletException { 
    Collection<Distritos> distritosBuscados = distritosService.buscarFiltro(usuario);
    return new ResponseEntity<>(distritosBuscados, HttpStatus.OK);
}

Note that I added /{usuario} at the end of endpoint, that represents its parameter Long usuario and the value of this will be passed through the URL.

Example: localhost:8080/api/distritos/1, where 1 must be the id value of your logged in user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.