0

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.

1 Answers1

0

You are missing a con = database.DatabaseConnect();, this causes the one con you use to be null. The other is set, but you don't copy the value to the other.

The Connection returned from the database.DatabaseConnect(); and is not saved to a local variable or instance level variable before accessing it. So it throws a NullPointerException

Change the second line in the below code

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

to

Login_System database = new Login_System();
con = database.DatabaseConnect(); // or Connection con = database.DatabaseConnect();

String sql = "SELECT StudentID, Course, VoteProgression FROM Voters";
SQLStatement= con.prepareStatement(sql); //this is where the errors appears to according to netbeans
Valamburi M
  • 692
  • 4
  • 17
Marged
  • 10,577
  • 10
  • 57
  • 99
  • @ValamburiM I just wanted to set him on the right track and not provide a complete solution but if you prefer I think this is ok ;-) – Marged Oct 29 '15 at 13:28