nullException when setting bank data as parameter

0

I'm trying to bring the data from the database with this method from my DAO class:

public List<Transacoes> listDBPF(){

    Connection con = new ConnectionFactory().getConnection();
    ResultSet res;
    List<Transacoes> arrTrans = new ArrayList<>();
    try {
        //view
        String sql = "SELECT * FROM vw_showTransacoes ORDER by data DESC";
        PreparedStatement stmt = con.prepareStatement(sql);
        //Executa a consulta
        res = stmt.executeQuery();
        //Execute o laço enquanto houver registros
        while(res.next()){

            Transacoes trans = new Transacoes();
            trans.getPessoaFisica().setNome(res.getString("nome"));
            trans.getServicos().setTipo(res.getString("tipoServico"));
            trans.getServicos().setValor(res.getDouble("valor"));
            trans.setObservacao(res.getString("observacao"));
            trans.setData(res.getString("data"));
            arrTrans.add(trans);
        }

    } catch (SQLException e){
        JOptionPane.showMessageDialog(null, "ERROR:"+e.getMessage()+". O sistema detectou um erro interno, por favor contact o ADM");
    }

    return arrTrans;
}

Follow Bean:

public class Transacoes {

    private PessoaFisica pessoaFisica;
    private PessoaJuridica pessoaJuridica;
    private Servicos servicos; 
    private String observacao;
    private String data;
    public PessoaFisica getPessoaFisica() {
         return pessoaFisica;
    }
    public void setPessoaFisica(PessoaFisica pessoaFisica) {
         this.pessoaFisica = pessoaFisica;
    }
    public String getData() {
         return data;
    }
    public void setData(String data) {
         this.data = data;
    }
    public PessoaJuridica getPessoaJuridica() {
         return pessoaJuridica;
    }
    public void setPessoaJuridica(PessoaJuridica pessoaJuridica) {
        this.pessoaJuridica = pessoaJuridica;
    }
    public Servicos getServicos() {
        return servicos;
    }
    public void setServicos(Servicos servicos) {
        this.servicos = servicos;
    }
    public String getObservacao() {
        return observacao;
    }
    public void setObservacao(String observacao) {
        this.observacao = observacao;
    }
  }

I do not know what I'm doing wrong

  

Exception in thread "main" java.lang.NullPointerException at   br.transactions.TransactionsDAO.listDBPF (TransactionsDAO.java:42) at   br.com.transacoes.TransacoesDAO.main (TransaçõesesO.java:89)   /home/raphael/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java   returned: 1 BUILD FAILED (total time: 0 seconds)

    
asked by anonymous 21.05.2017 / 02:17

1 answer

2

The problem is that you are trying to access null references of objects of classes PessoaFisica and Servicos .

When doing:

Transacoes trans = new Transacoes();

You create an object of Transacoes , but not of the objects within it. There are two ways to resolve this. One of them is creating the objects while filling them in while :

while(res.next()){

    Transacoes trans = new Transacoes();
    PessoaFisica pf =  new PessoaFisica();
    Servicos svc = new Servicos();

    pf.setNome(res.getString("nome"));
    trans.setPessoaFisica(pf);

    svc.setTipo(res.getString("tipoServico"));
    svc.setValor(res.getDouble("valor"));
    trans.setServicos(svc);

    trans.setObservacao(res.getString("observacao")); trans.setData(res.getString("data"));
    arrTrans.add(trans);
}

Or by adding a constructor in Transacoes and creating these objects directly in it, then you do not need to modify the code of while :

public Transacoes(){

    this.pessoaFisica = new PessoaFisica();
    this.pessoaJuridica = new PessoaJuridica();
    this.servicos = new Servicos();

}
    
21.05.2017 / 03:06