Error while listing data in Datatable

Asked

Viewed 48 times

0

Good morning, I’m trying to make a simple listing of a table on a primefaces datatable, but they’re not listing anything. Follow code until the moment.

.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="/WEB-INF/inc/template.xhtml">
    <ui:define name="titulo">
        <h:outputText value=" - Home" />
    </ui:define>
    <ui:define name="conteudo">
        <div class='wrp-content'>

            <f:view>
                <f:event listener="#{listPlanilhas.onLoad}" type="preRenderView"></f:event>
            </f:view>
            <h:form style="margin-top: 100px" id="formListagem">
                <p:dataTable id="criterios_multiskill" var="lista"
                    value="#{listPlanilhas.listPlanilha.listPlanilha}" paginator="true"
                    rows="10" rowsPerPageTemplate="5, 10, 20">

                    <p:column headerText="ID">
                        <h:outputText value="#{lista.id}" />
                    </p:column>
                    <p:column headerText="Documento">
                        <h:outputText value="#{listPlanilhas.listPlanilha.listPlanilha}" />
                    </p:column>
                    <p:column headerText="Nome">
                        <h:outputText value="#{lista.nome}" />
                    </p:column>
                    <p:column headerText="Empresa Cliente">
                        <h:outputText value="#{lista.empresa_cliente}" />
                    </p:column>
                    <p:column headerText="Empresa Assessoria">
                        <h:outputText value="#{lista.empresa_assessoria}" />
                    </p:column>
                    <p:column headerText="Carteira">
                        <h:outputText value="#{lista.carteira}" />
                    </p:column>
                    <p:column headerText="Produto">
                        <h:outputText value="#{lista.produto}" />
                    </p:column>
                    <p:column headerText="Tipo Operação">
                        <h:outputText value="#{lista.tipo_operacao}" />
                    </p:column>
                    <p:column headerText="Tipo Pessoa">
                        <h:outputText value="#{lista.tipo_pessoa}" />
                    </p:column>
                    <p:column headerText="Grupo">
                        <h:outputText value="#{lista.grupo}" />
                    </p:column>
                    <p:column headerText="Ação">
                        <h:outputText value="#{lista.acao}" />
                    </p:column>
                    <p:column headerText="Data Inclusão">
                        <h:outputText value="#{lista.criado_em}" />
                    </p:column>
                </p:dataTable>
            </h:form>
        </div>
    </ui:define>
</ui:composition>
</html>


   Bean: 

package br.com.racionalgames.pta.mbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.com.racionalgames.pta.dao.ListPlanilhasDao;
import br.com.racionalgames.pta.model.UploadFiles;

@ManagedBean(name = "listPlanilhas")
@ViewScoped
public class ListPlanilhasMBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private ListPlanilhasDao listPlanilha;
    private List<UploadFiles> lista;

    public void onLoad() throws Exception {

        try {

            List<UploadFiles> lista = new ArrayList<>();

            lista = listPlanilha.getListPlanilha();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error: " + e);
        }

    }

    public List<UploadFiles> getLista() {
        return lista;
    }

    public void setLista(List<UploadFiles> lista) {
        this.lista = lista;
    }

    public ListPlanilhasDao getListPlanilha() {
        return listPlanilha;
    }

    public void setListPlanilha(ListPlanilhasDao listPlanilha) {
        this.listPlanilha = listPlanilha;
    }

}

DAO:

package br.com.racionalgames.pta.dao;

import java.util.List;

import br.com.racionalgames.pta.model.UploadFiles;

public interface ListPlanilhasDao {

    List<UploadFiles> getListPlanilha() throws Exception;

}




package br.com.racionalgames.pta.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import br.com.racionalgames.pta.model.UploadFiles;

@Repository("ListPlanilhasDao")
@Transactional
@EnableTransactionManagement
public abstract class ListPlanilhasDaoImpl implements ListPlanilhasDao {

    private List<UploadFiles> listPlanilha = new ArrayList<>();

    @Override
    public List<UploadFiles> getListPlanilha() throws Exception {

        List<UploadFiles> lista = new ArrayList<>();

        String sql = "SELECT * FROM pta.criterios_multiskill";

        ResultSet rs = (ResultSet) sessionFactory.getCurrentSession().createQuery(sql).list();

        return (List<UploadFiles>) rs;
    }

    public void setListPlanilha(List<UploadFiles> listPlanilha) {

        this.listPlanilha = listPlanilha;

    }

    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

In the console this generating this error.

java.lang.Nullpointerexception at br.com.racionalgames.pta.mbean.ListPlanageMBean.onload(Listplanilhasmbean.java:28)

The method that is in error is this one

public void onLoad() throws Exception {

        try {

            List<UploadFiles> lista = new ArrayList<>();

               //LINHA DO ERRO
            lista = listPlanilha.getListPlanilha();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error: " + e);
        }

    }

1 answer

0

         @Override
            public List<UploadFiles> getListPlanilha() throws Exception {

                List<UploadFiles> lista = new ArrayList<>();
                Criteria crit =        
 sessionFactory.getCurrentSession().createCriteria(UploadFiles.class);
      List results = crit.list(); 
                return (List<UploadFiles>) results;
            }

In your bean Managed changes this line of:

private List<UploadFiles> lista;

for:

private List<UploadFiles> lista = new ArrayList<>();

Browser other questions tagged

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