0
I’m developing an interface that should return in a JTable
values of a mysql table. I have developed the following method:
public class Teste extends javax.swing.JFrame {
private JTable table;
DefaultTableModel modelo = new DefaultTableModel();
public Teste() {
initComponents();
}
public void popularJtable(String sql) {
try {
Connection con = new ConnectionFactory().getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
modelo = (DefaultTableModel) table.getModel();
modelo.addColumn("tipoCurso");
modelo.addColumn("nomeCurso");
while (rs.next()) {
modelo.addRow(new Object[]{
rs.getString("tipoCurso"),
rs.getString("nomeCurso")
});
}
table = new JTable(modelo);
jScrollPaneCursos = new JScrollPane(table);
stmt.close();
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPaneCursos = new javax.swing.JScrollPane();
Pesquisar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Pesquisar.setText("Pesquisar");
Pesquisar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PesquisarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(59, 59, 59)
.addComponent(jScrollPaneCursos, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(146, 146, 146)
.addComponent(Pesquisar)))
.addContainerGap(77, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(93, Short.MAX_VALUE)
.addComponent(Pesquisar)
.addGap(18, 18, 18)
.addComponent(jScrollPaneCursos, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(66, 66, 66))
);
pack();
}// </editor-fold>
private void PesquisarActionPerformed(java.awt.event.ActionEvent evt) {
String sql = "SELECT tipoCurso, NomeCurso FROM curso";
popularJtable(sql);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Teste().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Pesquisar;
private javax.swing.JScrollPane jScrollPaneCursos;
}
The idea is to call this method inside a search button, where I pass a string with the mysql script (it is a select in this case). However, it points out the following error:
Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception
and points to the line:
modelo = (DefaultTableModel) table.getModel();
I redesigned the project, tested and the problem remains the same and in the same line. The table that was to appear was the course table, which has the columns idCurso, typeCurso and nomeCurso.
– Costa.Gustavo
On which line now?
– user28595
model = (Defaulttablemodel) table.getModel();
– Costa.Gustavo