Returning Nullpointer in controller

Asked

Viewed 61 times

0

I have a method in controller which has as its function to make a query in my bank and after returning me only one field of this search, however I am taking nullpointer in the first for that I’m using. I’d like help with that.

The constants are these:

private List<String> listaNomeProjeto;
private List<String> listaNomePerfil;
private List<String> listaNomeJornada;

@RequestMapping(value = REDIRECT_PAGE_CADASTRO, method = RequestMethod.GET)
    public ModelAndView viewCadastro(Model model) {

        List<Projeto> listaCompletaProjeto = projetoService.findAll();

        for (Projeto listaProjetos : listaCompletaProjeto) {
            listaNomeProjeto.add(listaProjetos.getProjeto());
        }

        List<Perfil> listaCompletaPerfil = perfilService.findAll();

        for (Perfil listaPerfis : listaCompletaPerfil) {
            listaNomePerfil.add(listaPerfis.getPerfil().toString());
        }

        List<Jornada> listaCompletaJornada = jornadaService.findAll();

        for (Jornada listaJornadas : listaCompletaJornada) {
            listaNomeJornada.add(listaJornadas.getDsJornada().toString());
        }

        usuarioBean = new UsuarioBean(listaNomeProjeto, listaNomePerfil, listaNomeJornada);

        model.addAttribute("usuarioBean", usuarioBean);

        return new ModelAndView(REQUEST_MAPPING_PAGE_CADASTRO);
    }

Thanks in advance!

  • 1

    Copy and paste the stacktrace entire error in your question, otherwise it becomes difficult to know what the problem is

1 answer

2


You are not initiating your lists, so at the moment the method add is called for the variable listaNomeProjeto, an exception is triggered since it is null. One way to correct by disregarding your business rule (which is not clear) is to replace the statements to the following:

private List<String> listaNomeProjeto = new ArrayList<>();
private List<String> listaNomePerfil = new ArrayList<>();
private List<String> listaNomeJornada = new ArrayList<>();

Maybe you should consider creating variables within your method:

@RequestMapping(value = REDIRECT_PAGE_CADASTRO, method = RequestMethod.GET)
public ModelAndView viewCadastro(Model model) {
  List<Projeto> listaCompletaProjeto = projetoService.findAll();
  List<String> listaNomeProjeto = new ArrayList<>();
  List<String> listaNomePerfil = new ArrayList<>();
  List<String> listaNomeJornada = new ArrayList<>();
  ...
}

Browser other questions tagged

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