基本的に、データベースからResultSetを取得する際に問題が発生します。このコードブロックの34行目(?)でエラーが発生しましたが、マークを付けました
ResultSet rs = caq.executeQuery("SELECT * FROM ProjectScore"); //error goes here
実行時にnullポインター例外が発生します。これは、以下のcatchステートメントからの出力です。
null, calculating average score failed
java.lang.NullPointerException
[Ljava.lang.StackTraceElement;@1de2b1
興味深いのは、最後にクラスでまったく同じ接続を使用し、そこでエラーが発生しないことです。ステートメントを最初のクラスにコピーすることも機能しないので、それは別のものだと思います。私が持っているのはそれだけだと思います、どんな助けでもありがたいです:)
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class MainWindow extends JFrame implements ActionListener{
//.......................
private int [] averageScore;
//References
private LogInWindow liw;
private NewUserWindow nuw;
private ScoreWindow sw;
private boolean isAnotherWindowOpen = false;
private boolean isLoggedIn = false;
private ConnectionAndQueries caq;
public MainWindow(ConnectionAndQueries caq) throws SQLException{
this.caq = caq;
//.................................
//Middle
averageScore = new int [9];
calculateAverageScore();
setTable();
//............................
}
private void calculateAverageScore() throws SQLException{
try{
ResultSet rs = caq.executeQuery("SELECT * FROM ProjectScore"); //error goes here
int [] count = new int [9];
int [] totalScore = new int [9];
while(rs.next()){
int itemID = rs.getInt("itemID");
count[itemID]++;
totalScore[itemID] += rs.getInt("Score");
}
for(int i = 0; i < 9; i++){
averageScore[i] = totalScore[i] / count[i];
}
}
catch (Exception e) {
System.out.print(e.getMessage());
System.out.println(", calculating average score failed");
System.out.println(e.toString());
System.out.println(e.getStackTrace().toString());
}
}
}
//next class
import java.sql.*;
public class ConnectionAndQueries {
private static Connection connection;
private static Statement statement;
private MainWindow mw;
public ConnectionAndQueries() throws ClassNotFoundException, SQLException{
mw = new MainWindow(this);
connect();
}
public static void connect() throws ClassNotFoundException, SQLException{
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://zzzzzzzzzz", "yyyy", "xxxx"); //dont think im allowed to give that info
statement = connection.createStatement();
} catch (Exception e) {
System.out.println("Connecting to the database failed");
}
}
public ResultSet executeQuery(String query) throws SQLException {
return statement.executeQuery(query);
}
public int executeUpdate(String update) throws SQLException {
return statement.executeUpdate(update);
}
public static void main(String [] args) throws ClassNotFoundException, SQLException{
ConnectionAndQueries caq = new ConnectionAndQueries();
}
}
//another class which uses the connection class, and works.
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class LogInWindow extends JFrame implements ActionListener{
//........................
//References
private MainWindow mw;
private ConnectionAndQueries caq;
public LogInWindow(MainWindow mw, ConnectionAndQueries caq){
this.mw = mw;
this.caq = caq;
//......................
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == logIn){
String usn = usernameField.getText();
String pwd = passwordField.getText();
try {
ResultSet rs = caq.executeQuery("SELECT * FROM ProjectCustomer");
while(rs.next()){
if(rs.getString("username").equals(usn)){
if(rs.getString("passwrd").equals(pwd)){
logInSuccess(usn);
mw.userLoggedIn(usn);
quit();
}
}
}
if(mw.isUserLoggedIn() == false)
logInFailed();
} catch (Exception e2) {
System.out.print(e2.getMessage());
System.out.println(", error at log in");
System.out.println(e2.toString());
}
}
else if(e.getSource() == quit){
quit();
}
}
//............................
}