Returning NullPointer to the controller

0

I have a method in controller that has the function to make a query in my database and after returning only one field of this search, however I am taking nullpointer in the first for that I am using. I would like help in 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);
    }

Thank you in advance!

    
asked by anonymous 01.02.2017 / 19:40

1 answer

2

You are not initializing your lists, so the add method is called for the listaNomeProjeto variable, an exception is thrown because it is null. One way to fix it by disregarding your business rule (which is not clear) is to replace the declarations for the following:

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

You may want to 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<>();
  ...
}
    
01.02.2017 / 19:57