i know there is a similar post as this and they have answered it already but when i try it on my code there still is an error and netbeans show that it is a AWT-EventQueue-0 java.lang.NullPointerException Error and ithink it is because of the con
variable. it declared in both class.
here is the code for DatabaseConnect() in my Login_System.java:
public Connection DatabaseConnect(){
try {
String host = "jdbc:mysql://localhost:3306/CPE541Project";
String userName = "root";
String userPass = "";
con = DriverManager.getConnection(host, userName, userPass);
return con;
}
catch (SQLException | HeadlessException err) {
javax.swing.JOptionPane.showMessageDialog(this, "Error:\n" + err);
}
return null;
}
there is no error when i run Login_System.java but when i try to login to to UI_Staff.java that is when the error starts, here is the snippet of the code from UI_Staff.java where the error appears:
private void ViewVotersTable(){
try{
Login_System database = new Login_System();
database.DatabaseConnect();
String sql = "SELECT StudentID, Course, VoteProgression FROM Voters";
SQLStatement= con.prepareStatement(sql); //this is where the errors appears to according to netbeans
queryResultSet = SQLStatement.executeQuery();
tbl_ElectionProgress.setModel(DbUtils.resultSetToTableModel(queryResultSet));
sql ="SELECT COUNT(*) from Voters";
SQLStatement= con.prepareStatement(sql);
queryResultSet = SQLStatement.executeQuery();
while(queryResultSet.next()){
NumberOfVoters= queryResultSet.getString("COUNT(*)");
}
lbl_NumberOfVoters.setText(NumberOfVoters);
sql ="SELECT COUNT(*) from Voters where VoteProgression='DONE'";
SQLStatement= con.prepareStatement(sql);
queryResultSet = SQLStatement.executeQuery();
while(queryResultSet.next()){
ProcessedVotes = queryResultSet.getString("COUNT(*)");
}
lbl_ProcessedVotes.setText(ProcessedVotes);
sql ="SELECT COUNT(*) from Voters where VoteProgression='NOT DONE'";
SQLStatement= con.prepareStatement(sql);
queryResultSet = SQLStatement.executeQuery();
while(queryResultSet.next()){
UnprocessedVotes = queryResultSet.getString("COUNT(*)");
}
lbl_UnprocessedVotes.setText(UnprocessedVotes);
}
catch ( SQLException | HeadlessException err ) {
javax.swing.JOptionPane.showMessageDialog(this,"Error:\n"+err);
}
}
here is there first line of error in the stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at cpe541project.UI_Staff.ViewVotersTable(UI_Staff.java:869)
at cpe541project.UI_Staff.formWindowActivated(UI_Staff.java:859)
at cpe541project.UI_Staff.access$000(UI_Staff.java:15)
at cpe541project.UI_Staff$1.windowActivated(UI_Staff.java:115)
please help. i don't know what the error is about.