ArrayList giving error

0

I'm doing a database program and I'm not able to make my list appear in jtable . I created the method and at the time I call it my method of error, can someone help me the code is down there?

Error:

  

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at presentation.Customer.formInternalFrameOpened (ClientClient.java: 593)

List method

public List<cliente>preencherTabela(){
        String sql = "select * from cliente";
        cliente c=new cliente();

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

         try(PreparedStatement stmt = conexao.prepareStatement(sql)){

            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                c.setIdcodigo(rs.getInt("idcodigo"));
                c.setNome(rs.getString("nome"));
                c.setEndereco(rs.getString("endereco"));
                c.setEmail(rs.getString("email"));
                c.setCidade(rs.getString("cidade"));
                c.setBairro(rs.getString("bairro"));
                c.setTelefone(rs.getString("telefone"));
                c.setCelualr(rs.getString("celular"));
                c.setCep(rs.getString("cep"));
                c.setRg(rs.getString("rg"));
                c.setCpf(rs.getString("cpf"));
                c.setDatanascimento(rs.getString("datanascimento"));
                c.setUf(rs.getString("uf"));
                c.setSexo(rs.getString("sexo"));
            }
        }catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Usuario nao encontrado" + ex);
        }
        return lista;
    }
}      

code to call the method

private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {                                         
        cliente c = new cliente();
        List<cliente> lista = new ArrayList<>();
        Conexao con=null;
        lista=con.preencherTabela();

        for(int i=0;i<lista.size();i++){
            c=lista.get(i);
            jPcad.setValueAt(c.getIdcodigo(), i, 0);
            jPcad.setValueAt(c.getNome(), i, 1);
        }

    }          
    
asked by anonymous 05.03.2017 / 15:40

1 answer

4

There are several problems with this code. One of the problems is that you are returning an empty list because the preencheTabela() method creates objects that come from the database, but are never added to the list. Add lista.add(c) at the end of while :

public List<cliente> preencherTabela(){
        String sql = "select * from cliente";
        cliente c=new cliente();

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

         try(PreparedStatement stmt = conexao.prepareStatement(sql)){

            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                c.setIdcodigo(rs.getInt("idcodigo"));
                c.setNome(rs.getString("nome"));
                c.setEndereco(rs.getString("endereco"));
                c.setEmail(rs.getString("email"));
                c.setCidade(rs.getString("cidade"));
                c.setBairro(rs.getString("bairro"));
                c.setTelefone(rs.getString("telefone"));
                c.setCelualr(rs.getString("celular"));
                c.setCep(rs.getString("cep"));
                c.setRg(rs.getString("rg"));
                c.setCpf(rs.getString("cpf"));
                c.setDatanascimento(rs.getString("datanascimento"));
                c.setUf(rs.getString("uf"));
                c.setSexo(rs.getString("sexo"));
                lista.add(c);
            }
        }catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Usuario nao encontrado" + ex);
        }
        return lista;
    }
}    

Another problem is that you assign null to the bank connection just before calling the above method:

private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {                                         
        cliente c = new cliente();
        List<cliente> lista = new ArrayList<>();
        Conexao con=null;
        lista=con.preencherTabela();

        for(int i=0;i<lista.size();i++){
            c=lista.get(i);
            jPcad.setValueAt(c.getIdcodigo(), i, 0);
            jPcad.setValueAt(c.getNome(), i, 1);
        }

}   

No connection, con.preencherTabela(); will not work, it will pop nullpointerexception . Start the connection so that the list can be returned.

    
05.03.2017 / 15:49