gpt4 book ai didi

Java 空指针

转载 作者:行者123 更新时间:2023-12-01 16:40:23 26 4
gpt4 key购买 nike

有谁知道为什么当我从 MyServ2 类调用 getResultSet() 方法时出现空指针错误

这是我的 DBClass(省略导入等)

public DBClass(){

}

public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{

try
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);

return conn;

}
catch (Exception e)
{
e.printStackTrace();

return null;
}
}



public ResultSet getResultSet(String query){

try{
stmt = conn.createStatement();
result = stmt.executeQuery(query);

} catch(Exception e){
e.printStackTrace();
return null;
}
return result;

}



}

这是我的 MyServ2 类

public class MyServ2 extends HttpServlet {
private static final long serialVersionUID = 1L;
private DBClass db;

public MyServ2() {
super();
db = new DBClass();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


ResultSet rs = db.getResultSet("Select * from ....ect");
try {
while(rs.next()){
System.out.println(rs.getString(1).toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
}

}

最佳答案

您没有调用db.dbConnect(),因此db.conn将为空。

关于Java 空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4285231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com