I have this error and I can not find the problem.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at windows.WebPage.getUserList (MainPage.java:53) at windows.TelePrincipal.Show_Users_In_JTable (MainPage.java:69) at windows.HomePrincipal. (MainPage.java:28) at windows.Tap.main.lambda $ main $ 0 (Main.java:256) at java.awt.event.InvocationEvent.dispatch (InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl (EventQueue.java:756) at java.awt.EventQueue.access $ 500 (EventQueue.java:97) at java.awt.EventQueue $ 3.run (EventQueue.java:709) at java.awt.EventQueue $ 3.run (EventQueue.java:703) at java.security.AccessController.doPrivileged (Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent (EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:93) at java.awt.EventDispatchThread.run (EventDispatchThread.java:82)
Main Class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package janelas;
import classes.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
/**
*
* @author danilo
*/
public final class TelaPrincipal extends javax.swing.JFrame {
/**
* Creates new form TelaPrincipal
*/
public TelaPrincipal() {
initComponents();
Show_Users_In_JTable();
}
public Connection getConnection(){
Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sistema_clientes","danilo","2701232");
return con;
} catch (ClassNotFoundException | SQLException e){
return null;
}
}
public ArrayList<User> getUserList(){
ArrayList<User> usersList = new ArrayList<>();
Connection connection = getConnection();
String query = "SELECT * FROM 'users'";
Statement st;
ResultSet rs;
try{
st = connection.createStatement();
rs = st.executeQuery(query);
User user;
while(rs.next()){
user = new User(rs.getInt("id"),rs.getString("first_name"),rs.getString("last_name"),rs.getInt("age"));
usersList.add(user);
}
} catch(SQLException e){
}
return usersList;
}
// Display Data in JTable
public void Show_Users_In_JTable(){
ArrayList<User> list = getUserList();
DefaultTableModel model = (DefaultTableModel)jTable_Display_Users.getModel();
Object[] row = new Object[4];
for(int i = 0; i < list.size();i++){
row[0] = list.get(i).getId();
row[1] = list.get(i).getFirstName();
row[2] = list.get(i).getLastName();
row[3] = list.get(i).getAge();
model.addRow(row);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable_Display_Users = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jTextField_Id = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField_FirstName = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField_LastName = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jTextField_Age = new javax.swing.JTextField();
jButton_Insert = new javax.swing.JButton();
jButton_Update = new javax.swing.JButton();
jButton_Delete = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable_Display_Users.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Id", "First Name", "Last Name", "Age"
}
));
jScrollPane1.setViewportView(jTable_Display_Users);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(153, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(52, Short.MAX_VALUE))
);
jLabel1.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jLabel1.setText("Id:");
jTextField_Id.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jLabel2.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jLabel2.setText("First Name:");
jTextField_FirstName.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jLabel3.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jLabel3.setText("Last Name:");
jTextField_LastName.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jLabel4.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jLabel4.setText("Age:");
jTextField_Age.setFont(new java.awt.Font("Dialog", 0, 18)); // NOI18N
jButton_Insert.setText("Insert");
jButton_Update.setText("Update");
jButton_Delete.setText("Delete");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField_Id, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField_FirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField_LastName, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField_Age, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 1, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton_Insert)
.addGap(18, 18, 18)
.addComponent(jButton_Update)
.addGap(18, 18, 18)
.addComponent(jButton_Delete)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField_Id, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField_FirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField_LastName, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField_Age, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(102, 102, 102)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton_Insert)
.addComponent(jButton_Update)
.addComponent(jButton_Delete))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new TelaPrincipal().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton_Delete;
private javax.swing.JButton jButton_Insert;
private javax.swing.JButton jButton_Update;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable_Display_Users;
private javax.swing.JTextField jTextField_Age;
private javax.swing.JTextField jTextField_FirstName;
private javax.swing.JTextField jTextField_Id;
private javax.swing.JTextField jTextField_LastName;
// End of variables declaration
}
User class
package classes;
public class User{
//Atributos
private int id,age;
private String firstName, lastName;
//Métodos Especiais
public User(int id,String firstName, String lastName, int age) {
this.id = id;
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}