0
I am trying to delete several lines in my data table at once and get nullPointerException. I am sending the code
Bean
@Named
@ViewScoped
public class PesquisaFamiliasBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Familias familias;
@Inject
private Empresas empresas;
private List<Familia> listaDeFamilias = new ArrayList<>();
private Familia familiaSelecionada;
private List<Familia> familiasSelecionadas;
public List<Familia> getListaDeFamilias() {
return listaDeFamilias;
}
public FamiliaLazyList getLazyFamilias() {
return lazyFamilias;
}
public void setListaDeFamilias(List<Familia> listaDeFamilias) {
this.listaDeFamilias = listaDeFamilias;
}
public void setLazyFamilias(FamiliaLazyList lazyFamilias) {
this.lazyFamilias = lazyFamilias;
}
private List<Empresa> empresasRaizes = new ArrayList<>();
public List<Empresa> getEmpresasRaizes() {
return empresasRaizes;
}
@Inject
private CadastroFamiliaService cadastroFamiliaService;
private Familia novaFamilia = new Familia();
public Familia getNovaFamilia() {
return novaFamilia;
}
public void setNovaFamilia(Familia novaFamilia) {
this.novaFamilia = novaFamilia;
}
private FamiliaLazyList lazyFamilias;
public List<Familia> getFamilias() {
return listaDeFamilias;
}
public void inicializar() {
if (FacesUtil.isNotPostback()) {
lazyFamilias = new FamiliaLazyList(familias);
empresasRaizes = empresas.todasEmpresas();
}
}
public void doCreateFamlia() {
cadastroFamiliaService.salvar(novaFamilia);
}
public void doUpdateFamilia(ActionEvent actionEvent) {
cadastroFamiliaService.salvar(familiaSelecionada);
}
/**
*
* @param actionEvent
*/
public void doDeleteFamilias(ActionEvent actionEvent) {
for (Familia fami : familiasSelecionadas) {
System.out.println(fami.getDescricao());
}
familias.removerFamilias(familiasSelecionadas);
}
public void excluir() {
familias.remover(familiaSelecionada);
this.listaDeFamilias.remove(familiaSelecionada);
FacesUtil.addInfoMessage("Familia " + familiaSelecionada.getDescricao() + " excluída com sucesso.");
}
public Familia getFamiliaSelecionada() {
return familiaSelecionada;
}
public void setFamiliaSelecionada(Familia familiaSelecionada) {
this.familiaSelecionada = familiaSelecionada;
}
public List<Familia> getFamiliasSelecionadas() {
return familiasSelecionadas;
}
public void setFamiliasSelecionadas(List<Familia> familiasSelecionadas) {
this.familiasSelecionadas = familiasSelecionadas;
}
public FamiliaLazyList getLazyModel() {
return lazyFamilias;
}
}
Repository where deletion is made:
@Transactional
public void removerFamilias(Familia[] familias) {
for (Familia familia : familias) {
try {
familia = porId(familia.getCodigo());
manager.remove(familia);
manager.flush();
} catch (PersistenceException e) {
throw new NegocioException("Familia não pode ser excluída.");
}
}
}
datatable:
<p:dataTable id="familiasTable"
value="#{pesquisaFamiliasBean.lazyModel}" var="familia"
style="margin-top: 20px;" selection="#{pessaFamiliasBean.familiasSelecionadas}"
emptyMessage="Nenhuma Familia Encontrada"
paginatorPosition="bottom"
rows="20"
paginator="true"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
currentPageReportTemplate="(#{msg.pagina} {currentPage} #{msg.de} {totalPages}) - (#{msg.registro} {startRecord} #{msg.a} {endRecord}) - Total ({totalRecords} #{msg.registros})"
paginatorAlwaysVisible="true" rowsPerPageTemplate="10, 20, 30"
lazy="true">
<f:facet name="header">
#{msg.lista_linhas}
</f:facet>
<p:column selectionMode="multiple" style="width:18px" />
<p:column
sortBy="#{familia.descricao}">
<f:facet name="header">
<h:outputText value="Descrição da Familia" />
</f:facet>
<p:commandLink value="#{familia.descricao}"
update=":userDetailForm:display" oncomplete="userDialog.show()"
title="View">
<f:setPropertyActionListener value="#{familia}"
target="#{pesquisaFamiliasBean.familiaSelecionada}" />
</p:commandLink>
</p:column>
<p:column headerText="Descrição Empresa">
<h:outputText value="#{familia.empresa.nome}" />
</p:column>
<f:facet name="footer">
<p:commandButton value="New User"
oncomplete="newUserDialog.show()" icon="ui-icon-star"
title="Criar Nova Familia" />
<p:commandButton value="Deletar Familias" process="@this"
actionListener="#{pesquisaFamiliasBean.doDeleteFamilias}"
update="familiasTable" icon="ui-icon-trash" />
</f:facet>
</p:dataTable>
Where Nullpointerexception happens?
– igventurelli
It occurs in the for(Family Family : familiesSelected){}
– user2509556
Are you initiating the
familiasSelecionadas
?– igventurelli
made the following private Familia[] familiesSlecionadas; com seus getters e setters
– user2509556
Enter the entire Managedbean code
– igventurelli
edited the main code by placing the bean Managed
– user2509556
Pq are you using array? Use list instead of array and initialize it
– igventurelli
already tried and gave the same error . you see something wrong in the code?
– user2509556
Put the updated code. You initialized the list?
– igventurelli
with list @92,54 actionListener="#{searchFamiliasBean.doDeleteFamilias}": java.lang.Nullpointerexception
– user2509556
updated the list
– user2509556
Private list<Family> familiesSelected = new Arraylist<>(); nullPointerException does not occur, but does not exclude.
– user2509556