Error to popular jtable with database

0

I have to make a client registration application in JAVA, I'm using eclipse. The problem is in the listarCli() method, because when I comment on this method the error does not appear. The error is: "java.lang.NullPointerException"

According to Google this error is for when something was not instantiated, but I can not find where I went wrong.

Can anyone help me?

code:

package View;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import Factory.Conexao;
import Model.ModelCliente;
import net.proteanit.sql.DbUtils;

import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TelaCliente extends JDialog {

    private final JPanel contentPanel = new JPanel();
    public JTextField txtNome, txtCpf, txtTelefone, txtEndereco, txtPesquisar;
    public JButton btnInserir, btnAlterar, btnExcluir, btnSair, btnPesquisar;
    private JTable table;
    private JMenuBar menuBar;
    private JMenu mnOpc;
    private JMenuItem mntmCadastrar, mntmAlterar, mntmSair;
    DefaultTableModel modelTable;
    Conexao con;

    /**
     * Launch the application.
     */
    /*public static void main(String[] args) {
        try {
            TelaCliente dialog = new TelaCliente();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    /**
     * Create the dialog.
     */
    public TelaCliente(ModelCliente model) {
        setTitle("Cliente");
        setBounds(100, 100, 450, 408);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JLabel lblNome = new JLabel("Nome:");
            lblNome.setBounds(10, 38, 46, 14);
            contentPanel.add(lblNome);
        }
        {
            txtNome = new JTextField();
            txtNome.setBounds(66, 35, 178, 20);
            contentPanel.add(txtNome);
            txtNome.setColumns(10);
        }
        {
            JLabel lblCpf = new JLabel("CPF:");
            lblCpf.setBounds(262, 35, 46, 14);
            contentPanel.add(lblCpf);
        }
        {
            txtCpf = new JTextField();
            txtCpf.setBounds(291, 32, 112, 20);
            contentPanel.add(txtCpf);
            txtCpf.setColumns(10);
        }
        {
            JLabel lblTelefone = new JLabel("Telefone:");
            lblTelefone.setBounds(10, 69, 52, 14);
            contentPanel.add(lblTelefone);
        }
        {
            txtTelefone = new JTextField();
            txtTelefone.setBounds(66, 66, 86, 20);
            contentPanel.add(txtTelefone);
            txtTelefone.setColumns(10);
        }
        {
            JLabel lblEndereco = new JLabel("Endereco:");
            lblEndereco.setBounds(162, 69, 62, 14);
            contentPanel.add(lblEndereco);
        }
        {
            txtEndereco = new JTextField();
            txtEndereco.setBounds(234, 66, 169, 20);
            contentPanel.add(txtEndereco);
            txtEndereco.setColumns(10);
        }
        {
            btnInserir = new JButton("Inserir");
            btnInserir.setBounds(10, 109, 89, 23);
            contentPanel.add(btnInserir);
        }
        {
            btnAlterar = new JButton("Alterar");
            btnAlterar.setBounds(109, 109, 89, 23);
            contentPanel.add(btnAlterar);
        }
        {
            btnExcluir = new JButton("Excluir");
            btnExcluir.setBounds(217, 109, 89, 23);
            contentPanel.add(btnExcluir);
        }
        {
            btnSair = new JButton("Sair");
            btnSair.setBounds(316, 109, 89, 23);
            contentPanel.add(btnSair);
        }
        {
            JLabel lblPesquisar = new JLabel("Pesquisar:");
            lblPesquisar.setBounds(10, 146, 70, 14);
            contentPanel.add(lblPesquisar);
        }
        {
            txtPesquisar = new JTextField();
            txtPesquisar.setBounds(77, 143, 147, 20);
            contentPanel.add(txtPesquisar);
            txtPesquisar.setColumns(10);
        }
        {
            btnPesquisar = new JButton("Pesquisar");
            btnPesquisar.setBounds(240, 142, 97, 23);
            contentPanel.add(btnPesquisar);
        }
        {
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(27, 190, 364, 151);
            contentPanel.add(scrollPane);

            modelTable = new DefaultTableModel();
            modelTable.addColumn("CPF");
            modelTable.addColumn("Nome");
            modelTable.addColumn("Telefone");
            modelTable.addColumn("Endereco");
            table = new JTable(modelTable);
            //table = new JTable();
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    selecionaLinha();
                }
            });
            scrollPane.setViewportView(table);

            // MENU BAR
            menuBar = new JMenuBar();
            menuBar.setBounds(0, 0, 487, 21);
            contentPanel.add(menuBar);

            mnOpc = new JMenu("Opções");
            menuBar.add(mnOpc);

            mntmCadastrar = new JMenuItem("Cadastrar");
            mnOpc.add(mntmCadastrar);

            mntmAlterar = new JMenuItem("Alterar/Excluir");
            mnOpc.add(mntmAlterar);

            mntmSair = new JMenuItem("Sair");
            mnOpc.add(mntmSair);

            enableOffAll();
            //listarCli();
        }       
    }

    // ACTIONS ========================================================

    public void addInserirListener(ActionListener cal) {
        btnInserir.addActionListener(cal);
    }

    public void addAlterarListener(ActionListener cal) {
        btnAlterar.addActionListener(cal);
    }

    public void addExcluirListener(ActionListener cal) {
        btnExcluir.addActionListener(cal);
    }

    public void addSairListener(ActionListener cal) {
        btnSair.addActionListener(cal);
    }

    public void addPesquisarListener(ActionListener cal) {
        btnPesquisar.addActionListener(cal);
    }

    /*public void addMnInserirListener(ActionListener cal) {
        mntmCadastrar.addActionListener(cal);
    }

    public void addMnAlterarListener(ActionListener cal) {
        mntmAlterar.addActionListener(cal);
    }

    public void addMnSairListener(ActionListener cal) {
        mntmSair.addActionListener(cal);
    }*/

    //  GET & SETS ===========================================================

    public String getNome() {
        return txtNome.getText();
    }

    public void settNome(String nome) {
        txtNome.setText(nome);
    }

    public String getCPF() {
        return txtCpf.getText();
    }

    public void setCPF(String cpf) {
        txtCpf.setText(cpf);
    }

    public String getTelefone() {
        return txtTelefone.getText();
    }

    public void setTelefone(String tel) {
        txtTelefone.setText(tel);;
    }

    public String getEndereco() {
        return txtEndereco.getText();
    }

    public void setEndereco(String end) {
        txtEndereco.setText(end);
    }

    public String getPesquisar() {
        return txtPesquisar.getText();
    }

    public void setPesquisar(String pesq) {
        txtPesquisar.setText(pesq);
    }

    // ENABLES ===========================================================

    public void enableOffAll() {
        txtCpf.setEnabled(false);
        txtNome.setEnabled(false);
        txtTelefone.setEnabled(false);
        txtEndereco.setEnabled(false);
        btnInserir.setEnabled(false);
        btnAlterar.setEnabled(false);
        btnExcluir.setEnabled(false);
    }

    // LIMPAR CAMPOS =====================================================

    public void limpar() {
        txtCpf.setText("");
        txtNome.setText("");
        txtTelefone.setText("");
        txtEndereco.setText("");
    }

    //  POPULAR TABELA =====================================================
    public void listarCli(){

        String sql ="select cpf as CPF, nome as Nome, telefone as Telefone, endereco as Endereco from clientes;";

        try{
            ResultSet rs;
            PreparedStatement stm = con.conexao.prepareStatement(sql);
            //con.conecta();
            rs = stm.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Erro: "+e);
            }
    }

    // SELECIONAR LINHA DA TABELA ==============================================

    public void selecionaLinha(){
        int seleciona = table.getSelectedRow();
        txtCpf.setText(table.getModel().getValueAt(seleciona, 0).toString());
        txtNome.setText(table.getModel().getValueAt(seleciona, 1).toString());
        txtTelefone.setText(table.getModel().getValueAt(seleciona, 2).toString());
        txtEndereco.setText(table.getModel().getValueAt(seleciona, 3).toString());
    }   
}
    
asked by anonymous 15.09.2017 / 18:31

1 answer

1

You did not submit a Minimum, Full, and Verifiable example , so you can not draw more conclusions, but by looking at the code, you may notice that you declare a connection variable called con but not the instance.

I think you need to instantiate a connection, it's usually done like this:

con = DriverManager.getConnection("url do banco", "usuario do banco", "senha do banco");

I recommend reading from this post about using try-with-resources for database connection treatments.

    
15.09.2017 / 19:10