nullException when setting database data as parameter

Asked

Viewed 61 times

0

I am trying to bring the database data with this method of 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 the 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 don’t know what I’m doing wrong

Exception in thread "main" java.lang.Nullpointerexception at br.com.transactionsDAO.listDBPF(Transactionsdao.java:42) at br.com.transactionsDAO.main(transaction.java:89) /home/Raphael/. cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 Seconds)

  • Where is line 42 of the Transaction class?

  • It is this trans.getServicos(). setTipo(res.getString("typeServico"));

  • All three give error: trans.getPessoaFisica(). setName(res.getString("name")); trans.getServicos(). setTipo(res.getString("Servic type"); trans.getServicos(). setValor(res.getDouble("value"));

  • Notice that at no time do you create a PessoaFisica or Servicos in class Transacoes? This is the cause of the error.

  • No, it is not causing any error

1 answer

2


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

In doing:

Transacoes trans = new Transacoes();

you create an object of Transacoes, but not of the objects inside it. There are two ways to solve this. One of them is by creating the objects while filling them inside the 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 to Transacoes and creating these objects right on it, so you don’t need to modify the code of the while:

public Transacoes(){

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

}
  • Had made the top solution worked, was going to post here. But seeing the builder found much better. Thanks helped a lot

  • @Raphaelmoraes in fact it is more elegant and reduces your work every time you need to bring data from the database or create an object Transactions. :)

Browser other questions tagged

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