NullPointerException when calling method return in another class

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 already debugged and the method return is coming what I want, an id of type Long .

Returning this method, us , to the DistrictController class, when I call the request /distritos I get an error of java.lang.NullPointerException

DistrictController

 @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);
    } 
    
asked by anonymous 11.10.2017 / 18:31

1 answer

2

The exception happens because its resp attribute appears to be 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 id of the logged-in user on your client, for cookie or localStorage for example.

And so, you could tailor the buscarTodosDistritos() method of your DistritoController to receive the user id through the URL, as REST philosophy recommends :

@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 the endpoint , which represents its parameter Long usuario and its value will be passed through the URL.

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

    
11.10.2017 / 21:34