Well, I'm trying to populate a JTable with database data, but I've always encountered the same exception as java.lang.NullPointerException.
It should work as follows:
user clicks the refresh button and the program pulls the DB information, then allocates them to a table.
Note: I already managed to use this code in another program and it worked without errors.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at DAO.documentoDAO.SelectFull (documentDAO.java:39) at View.cadastro_documento $ 3.actionPerformed (cadastro_documento.java:115)
These are the classes I've created
file_document
package View;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import DAO.UsuarioDAO;
import DAO.documentoDAO;
import Model.Usuario;
import Model.documento;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
public class cadastro_documento extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
cadastro_documento frame = new cadastro_documento();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public cadastro_documento() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnArquivos = new JMenu("Arquivos");
menuBar.add(mnArquivos);
JMenuItem mntmSair = new JMenuItem("Sair");
mntmSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menu menu = new menu();
menu.setVisible(true);
dispose();
}
});
mnArquivos.add(mntmSair);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblTipoDocumento = new JLabel("Tipo Documento");
lblTipoDocumento.setBounds(10, 11, 101, 14);
contentPane.add(lblTipoDocumento);
textField = new JTextField();
textField.setBounds(121, 8, 149, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblJCadastrados = new JLabel("J\u00E1 Cadastrados");
lblJCadastrados.setBounds(10, 72, 94, 14);
contentPane.add(lblJCadastrados);
JPanel panel = new JPanel();
panel.setBounds(10, 92, 414, 158);
contentPane.add(panel);
panel.setLayout(null);
table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {},
new String[] {
"ID Documento", "Descri\u00E7\u00E3o"
}
));
table.setBounds(10, 141, 394, -129);
panel.add(table);
JButton btnCadastrar = new JButton("Cadastrar");
btnCadastrar.setBounds(10, 38, 105, 23);
contentPane.add(btnCadastrar);
JButton btnAtualizar = new JButton("Atualizar");
btnAtualizar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
documentoDAO u = new documentoDAO();
String[] colunas = { "idTipoDoc", "Tipo"};
DefaultTableModel dados = new DefaultTableModel(colunas, 2);
List<documento> documentos = u.SelectFull();
for (documento documento : documentos) {
dados.addRow(new String[] { "" + documento.getIdTipoDoc(), documento.getTipo()});
}
table.setModel(dados);
}
});
btnAtualizar.setBounds(163, 39, 101, 23);
contentPane.add(btnAtualizar);
}
}
document
package Model;
public class documento {
private int idTipoDoc;
private String Tipo;
public int getIdTipoDoc() {
return idTipoDoc;
}
public void setIdTipoDoc(int idTipoDoc) {
this.idTipoDoc = idTipoDoc;
}
public String getTipo() {
return Tipo;
}
public void setTipo(String tipo) {
Tipo = tipo;
}
public void add(documento doc) {
// TODO Auto-generated method stub
}
}
documentDAO
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Model.documento;
import Model.Usuario;
public class documentoDAO {
public boolean acesso;
private Connection con = null;
public void documentoDAO() {
this.con = new conexaoDAO().getConexao();
}
public void Inserir(documento doc) {
String sql = "INSERT INTO tipodoc(idTipoDoc, Tipo)VALUES (null,?)";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, doc.getTipo());
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<documento> SelectFull() {
String sql = "SELECT * FROM tipodoc";
try {
List<documento> documentos = new ArrayList<documento>();
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet result = stmt.executeQuery();
while (result.next()) {
documento doc = new documento();
doc.setIdTipoDoc(result.getInt("idTipoDoc"));
doc.setTipo(result.getString("Tipo"));
doc.add(doc);
}
result.close();
stmt.close();
return documentos;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}