Arraylist giving error

Asked

Viewed 143 times

0

I’m doing a little program with a database and I can’t seem to get my list to appear in one jtable. I created the method and at the time I call my method it error, someone can help me code this down?

Error:

Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception at presentcao.TelaCliente.formInternalFrameOpened(Telacliente . 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);
        }

    }          

1 answer

5

There are several problems in this code. One of the problems is that you are returning an empty list because the method preencheTabela() creates objects from the bank, but are never added to the list. Add a 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 connection to the bank 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);
        }

}   

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

  • You need a space between the return type and the method name": public List<client> fillTable() {

  • 1

    @n3uRoQuiLa this must have been error when copying here on the site, otherwise his code would not even compile and pop the nullpointerexception on the informed line.

  • 1

    Sure, it was just a sign.

  • how do I fix it, I couldn’t, can help me!!!

  • @Thijoe just make the suggested changes in the reply.

  • I did, but did not give, you can show please!!!

  • @Thijoe what problem occurs?

  • it does not occur any error now, more when I start my program , it was to load the lists in the table , more does not appear

  • 1

    @Thijoe ai is already another problem other than your original question. It would be interesting to end this by accepting the answer by clicking on v and create a new question with this new question, placing the link here as reference.

  • thanks brother , excuse the delay to respond , I am very busy and I have little time to respond , thanks brother

Show 5 more comments

Browser other questions tagged

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