我知道有一个类似的帖子,他们已经回答了它但是当我在我的代码上尝试它仍然有一个错误,netbeans显示它是一个AWT-EventQueue-0 java.lang.NullPointerException错误和ithink是因为con
变量。它在两个班级都宣布了。
这是我的Login_System.java中的DatabaseConnect()代码:
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;
}
当我运行Login_System.java时没有错误,但是当我尝试登录到错误开始时的UI_Staff.java时,这里是来自UI_Staff.java的代码片段,其中出现错误:
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);
}
}
这是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)
请帮忙。我不知道错误是什么。
答案 0 :(得分:0)
您缺少con = database.DatabaseConnect();
,这会导致您使用的con
为空。另一个已设置,但您不会将值复制到另一个。
从数据库返回的连接.DatabaseConnect();并且在访问之前不会保存到本地变量或实例级变量。所以它抛出一个NullPointerException
更改以下代码中的第二行
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
到
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