Java 正在PreparedStatement.close()中获取空指针
我试图使用jdbc将textfield中的值插入mySql,但我的问题是在关闭我准备好的语句时出现了空指针错误。这是我的密码Java 正在PreparedStatement.close()中获取空指针,java,Java,我试图使用jdbc将textfield中的值插入mySql,但我的问题是在关闭我准备好的语句时出现了空指针错误。这是我的密码 private Connection con; private Statement stm; private ResultSet rs; private PreparedStatement ps ; public void dbConnect(){ try{ Class.forName("com.mysql.jdbc.Driver")
private Connection con;
private Statement stm;
private ResultSet rs;
private PreparedStatement ps ;
public void dbConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbTestDrive"
+ "", "root", "password");
}
catch(ClassNotFoundException ex){}
catch(SQLException ex){}
}
public boolean insert(Member member)throws SQLException{
this.dbConnect();
boolean success = false;
String query = "INSERT INTO try VALUES(?, ?)";
try {
this.ps = this.con.prepareStatement(query);
this.ps.setString(1, member.getMemberId());
this.ps.setString(2, member.getFirstName());
int rows = this.ps.executeUpdate();
if(rows>0){
success = true;
}
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} finally{
this.ps.close(); // this is my line: 56 (im getting null pointer here)
this.con.close();
}
return success;
}
这是stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at rtu.tomis.dao.MemberDao.insert(MemberDao.java:56)
at rtu.tomis.gui.AddMemberView.btnSaveActionPerformed(AddMemberView.java:993)
at rtu.tomis.gui.AddMemberView.access$800(AddMemberView.java:21)
at rtu.tomis.gui.AddMemberView$9.actionPerformed(AddMemberView.java:476)
感谢您的帮助您应该这样编写代码:
finally{
if(ps != null)
try{
this.ps.close();
}catch(Exception e){
System.out.println("PreparedStatement close problem");
}
if(con != null)
try{
this.con.close();
}catch(Exception e){
System.out.println("Database Connection close problem");
}
}
如果任何con
或ps
由于某些错误而未初始化,这将避免空指针异常
此外,您还可以从所有代码中删除
这个
关键字,因为您的方法中没有任何与类变量同名的局部变量,因此不会有任何歧义您应该这样编写代码:
finally{
if(ps != null)
try{
this.ps.close();
}catch(Exception e){
System.out.println("PreparedStatement close problem");
}
if(con != null)
try{
this.con.close();
}catch(Exception e){
System.out.println("Database Connection close problem");
}
}
如果任何con
或ps
由于某些错误而未初始化,这将避免空指针异常
此外,您还可以从所有代码中删除
this
关键字,因为您的方法中没有任何与类变量同名的局部变量,因此不会有任何歧义您在try
块中得到异常,在此行执行之前或执行时:
this.ps = this.con.prepareStatement(query);
catch
块重新抛出异常后,块最终执行。不幸的是,因为ps
是null
,所以您会得到另一个异常,然后抛出该异常,隐藏原始异常
您需要更加防御性地编写代码,仅当ps
和con
不为null
时才关闭它们,并捕获close()
调用本身可能引发的任何异常。这将允许您传播由try
主体引发的任何异常
您也不应该默默地忽略dbConnect()
的try
块中抛出的异常。我怀疑异常是您问题的根源。在执行此行之前或期间,您在try
块中遇到异常:
this.ps = this.con.prepareStatement(query);
catch
块重新抛出异常后,块最终执行。不幸的是,因为ps
是null
,所以您会得到另一个异常,然后抛出该异常,隐藏原始异常
您需要更加防御性地编写代码,仅当ps
和con
不为null
时才关闭它们,并捕获close()
调用本身可能引发的任何异常。这将允许您传播由try
主体引发的任何异常
您也不应该默默地忽略dbConnect()
的try
块中抛出的异常。我怀疑有一个异常是您问题的根源。您需要在finally子句中检查null
finally{
if(ps != null)
this.ps.close();
//
}
但是潜在的问题可能是当您打开连接时,在连接失败后,它将不会初始化您准备好的语句:
this.ps = this.con.prepareStatement(query);
换句话说,在调用prepareStatement
之前,请确保con
不为空。您应该更改方法dbConnect
并打印异常,这将帮助您解决问题的原因
try{
Class.forName("com.mysql.jdbc.Driver");
this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbTestDrive"
+ "", "root", "password");
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
}
catch(SQLException ex){
ex.printStackTrace();
}
您需要在finally子句中检查null
finally{
if(ps != null)
this.ps.close();
//
}
但是潜在的问题可能是当您打开连接时,在连接失败后,它将不会初始化您准备好的语句:
this.ps = this.con.prepareStatement(query);
换句话说,在调用prepareStatement
之前,请确保con
不为空。您应该更改方法dbConnect
并打印异常,这将帮助您解决问题的原因
try{
Class.forName("com.mysql.jdbc.Driver");
this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbTestDrive"
+ "", "root", "password");
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
}
catch(SQLException ex){
ex.printStackTrace();
}
我确实认为在dbConnect()方法中引发了一个异常,这就是为什么jvm在读取准备好的语句的实例化之前直接读取finally。我确实认为在dbConnect()中引发了一个异常方法,这就是jvm在读取准备好的语句的实例化之前直接读取finally的原因。您没有在insert方法中获取准备好的语句对象并调用null上的任何内容会给您带来null指针异常。您没有在insert方法中获取准备好的语句对象并调用null上的任何内容都会导致null指针异常。您确定在finally块上方的catch块中没有引发异常吗?这似乎是ps可以为null的唯一情况。此外,您可以将所有这些连接相关的对象作为局部变量移动到insert方法,因为它们应该在每次代码中出现问题时创建(连接对象除外,它可以作为成员进行维护,但很棘手,具体取决于类的访问方式)<代码>1。
您正试图捕获并同时抛出插入方法的异常<代码>2。
不太重要,但成功
是布尔值
然后您可以随时从块中返回<代码>3
为什么要用this
引用所有内容,您不需要这样做。您确定finally块上方的catch块中没有引发异常吗?这似乎是ps可以为null的唯一情况。此外,您可以将所有这些连接相关的对象作为局部变量移动到insert方法,因为它们应该在每次代码中出现问题时创建(连接对象除外,它可以作为成员进行维护,但很棘手,具体取决于类的访问方式)<代码>1。您正试图捕获并同时抛出插入方法的异常<代码>2。
不太重要,但成功
是布尔值
然后您可以随时从块中返回<代码> 3 < /COD>为什么你使用“<代码> > <代码>引用所有的东西,你不需要这样做。也可以考虑在Test/catch块中封装Couter(),并记录可能发生的任何错误。我尝试你的代码@阿布,但是我得到了一个