英文:
Java JDBC CachedRowSet creation results in NullPointerException
问题
我正在努力创建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行。也就是createCachedRowSet
函数中的cachedRowSet.setUsername(connectionValues.userName);
。数据是正确的,我有一个测试打印方法,可以在代码中打印相关信息,它不仅可以无错误地提取相同的数据,而且还可以打印出正确的信息。所以我不确定哪里出错了。了解我,很可能是显而易见的,但我看不到它。
英文:
I'm working on creating a connection between a Java program and a MySQL database. The only thing not included in the code is the ConnectionValue
class which pulls all the relevant connection data needed for each specific connection/query. This class does work. When I run the code, it creates a 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;
}
}
The stack trace reports that the error is occurring at line 14. Which is the cachedRowSet.setUsername(connectionValues.userName);
in the function createCachedRowSet
. The data is correct, I have a test print method that prints the relevant information in the code and it not only pulls the same data without error but also prints the correct information. So I'm not sure what is wrong. Knowing me, it's probably obvious but I can't see it.
答案1
得分: 1
@TeaDrinker ... 请友情初始化在第7行中定义但尚未初始化的cachedRowSet对象,在第14行设置cachedRowSet属性时抛出了NullPointerException。
英文:
@TeaDrinker ... Kindly Initialize cachedRowSet object which you have defined in line 7 but not initialized so at line 14 when you are setting property of cachedRowSet its throwing NullPointerException.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论