0

Java プログラムと MySQL データベース間の接続の作成に取り組んでいます。コードに含まれていない唯一のものは、ConnectionValue特定の接続/クエリごとに必要なすべての関連する接続データを取得するクラスです。このクラスは機能します。コードを実行すると、NullPointerException.

import javax.sql.rowset.CachedRowSet;
import java.sql.*;

public class SQLCommands {
    private Connection connection;
    private PreparedStatement preparedStatement;
    private CachedRowSet cachedRowSet;

    public void createConnection(ConnectionValues connectionValues) throws SQLException {
        connection=DriverManager.getConnection(connectionValues.urlString,connectionValues.userName,connectionValues.password);
    }

    public void createCachedRowSet(ConnectionValues connectionValues, String query) throws SQLException {
        cachedRowSet.setUsername(connectionValues.userName);
        cachedRowSet.setPassword(connectionValues.password);
        cachedRowSet.setUrl(connectionValues.urlString);
        cachedRowSet.setCommand(query);
    }

    public void createPreparedStatement(CachedRowSet cachedRowSet) throws SQLException {
        preparedStatement=connection.prepareStatement(cachedRowSet.getCommand());
    }

    public CachedRowSet readDataBase(int dbID, String query) throws Exception {
        ConnectionValues connectionValues=new ConnectionValues(dbID);
        createConnection(connectionValues);
        System.out.println(connectionValues.urlString+"\n"+connectionValues.password+"\n"+connectionValues.userName);
        createCachedRowSet(connectionValues,query);
        createPreparedStatement(cachedRowSet);

        try {
            preparedStatement.execute();
            return cachedRowSet;
        }catch (Exception e){
            System.err.print("ERROR!\nFunction: readDataBase\nClass: SQLCommands\n");
            System.err.print(e);
        }finally {
            connection.close();
        }
        return cachedRowSet;
    }
}

スタック トレースは、14 行目でエラーが発生していることを報告しcachedRowSet.setUsername(connectionValues.userName);ますcreateCachedRowSet。データは正しいです。コード内の関連情報を印刷するテスト印刷メソッドがあり、エラーなしで同じデータをプルするだけでなく、正しい情報も印刷します。だから、何が悪いのかわからない。私を知っていると、それはおそらく明らかですが、私には見えません。

4

1 に答える 1