Problems with Nullpointerexception

Asked

Viewed 66 times

4

I am making a Java application using primefaces 5.1 and was working correctly the part of deleting students in version 2.1 before upgrading to version 5.1.

I am trying to delete a student, but this error appears. But when the page returns the student is excluded

I am 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>

That is the mistake:

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’s where the bug, 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 mistake is right here:

alunosFiltrados.remove(alunoSelecionado);

I’m calling remove here in 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.");
    }

}
  • 1

    alunosFiltrados is with the null value then.

1 answer

4

Start the value of alunosFiltrados in the declaration of the attribute:

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

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

  • 1

    Oh man. Thank you. It worked.

  • Since it helped you can recognize me by giving a vote in favor :-) if you think this is the definitive answer, mark it as the chosen answer, but know that possibly someone else might give a better answer. There are those who prefer to wait a week before scheduling the answer

Browser other questions tagged

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