Problems with NullPointerException

4

I'm doing a Java application using 5.1 primefaces and it was working correctly to delete students in version 2.1 before upgrading to version 5.1.

I'm trying to delete a student, but this error appears. But when you return to the page the student is excluded

I'm using the dialog to open a delete confirmation screen. This code below is the dialog

            <p:commandButton icon="ui-icon-trash" title="Excluir" immediate="true"
                oncomplete="PF('confirmacaoExclusao').show()" process="@this"
                update=":frmPesquisa:confirmacaoExclusaoDialog">
                <f:setPropertyActionListener value="#{aluno}"
                    target="#{pesquisaAlunoBean.alunoSelecionado}" />
            </p:commandButton>
        </p:column>
    </p:dataTable>

    <p:confirmDialog header="Exclusão de Aluno"
        message="Tem certeza que deseja excluir o(a) aluno(a) #{pesquisaAlunoBean.alunoSelecionado.nome}?"
        widgetVar="confirmacaoExclusao" id="confirmacaoExclusaoDialog">
        <p:button value="Não"
            onclick="PF('confirmacaoExclusao').hide(); return false;" />
        <p:commandButton value="Sim"
            oncomplete="PF('confirmacaoExclusao').hide();"
            action="#{pesquisaAlunoBean.excluir}" process="@this"
            update=":frmPesquisa:alunosTable" />
    </p:confirmDialog>

This is the error:

Caused by: java.lang.NullPointerException
at com.odontoclinicas.clinicas.controller.PesquisaAlunoBean.excluir(PesquisaAlunoBean.java:56)
at com.odontoclinicas.clinicas.controller.PesquisaAlunoBean$Proxy$$$WeldClientProxy.excluir(PesquisaAlunoBean$Proxy$$$WeldClientProxy.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 59 more

Control page

@Named
@ViewScoped
public class PesquisaAlunoBean implements Serializable {

private static final long serialVersionUID = 1L;

private List<Aluno> alunosFiltrados;

private LazyDataModel<Aluno> model;

@Inject
private Alunos alunos;

private AlunoFilter filtro;
private Aluno alunoSelecionado;

public PesquisaAlunoBean() {
    filtro = new AlunoFilter();
    model = new LazyDataModel<Aluno>() {

        private static final long serialVersionUID = 1L;

        @Override
        public List<Aluno> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                Map<String, Object> filters) {
            filtro.setPrimeiroRegistro(first);
            filtro.setQuantidadeRegistros(pageSize);

            setRowCount(alunos.quantidadeFiltrados(filtro));
            return alunos.filtrados(filtro);
        }

    };
}

public void excluir() {
    alunos.remover(alunoSelecionado);
    alunosFiltrados.remove(alunoSelecionado);

    FacesUtil.addInfoMessage("Aluno(a) " + alunoSelecionado.getNome() + " excluído(a) com sucesso.");
}

public List<Aluno> getAlunosFiltrados() {
    return alunosFiltrados;
}

public LazyDataModel<Aluno> getModel() {
    return model;
}

public AlunoFilter getFiltro() {
    return filtro;
}

public Aluno getAlunoSelecionado() {
    return alunoSelecionado;
}

public void setAlunoSelecionado(Aluno alunoSelecionado) {
    this.alunoSelecionado = alunoSelecionado;
}
}

And here is where the error is, on the control page.

public void excluir() {
    alunos.remover(alunoSelecionado);
    alunosFiltrados.remove(alunoSelecionado);

    FacesUtil.addInfoMessage("Aluno(a) " + alunoSelecionado.getNome() + " excluído(a) com sucesso.");
}

The error is exactly here:

alunosFiltrados.remove(alunoSelecionado);

I'm calling remove here in the class Students:

@Transactional
public void remover(Aluno aluno) {
    try {
        aluno = porId(aluno.getId());
        manager.remove(aluno);
        manager.flush();
    } catch (PersistenceException e) {
        throw new NegocioException("Aluno não pode ser excluído.");
    }

}
    
asked by anonymous 29.06.2017 / 01:54

1 answer

4

Start the value of alunosFiltrados in the attribute declaration:

private List<Aluno> alunosFiltrados = new ArrayList<>();

If you need to set this variable externally, put it in the constructor or a setter for it

    
29.06.2017 / 02:02