关闭statement时出现NullPointerException

laodaoke 2012-11-29 01:53:00
公司的一个系统出了个很奇怪的问题,方法调用Trace如下:

ABC.java
Java代码
......
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
......
try {
if(A){
theSQL = "******";
connection = theContext.theSQLMgr.getDBSession().getConnection();
ps = connection.prepareStatement(theSQL);
......
}
else{
theSQL = "******";
connection = theContext.theSQLMgr.getDBSession().getConnection();
ps = connection.prepareStatement(theSQL);
......
}
} catch (SQLException e) {
e.printStackTrace();
throw new DBResourceException(e);
} finally {
DBUtils.release(theContext, rs, ps, connection);
}

DBUtils.java
Java代码

public static void release(Context context, ResultSet rs, Statement ps, Connection con) {
if (rs != null) {
context.theSQLMgr.getDBSession().releaseResultSet(rs);
}
if (ps != null) {
context.theSQLMgr.getDBSession().releaseStatement(ps);
}
if (con != null)
context.theSQLMgr.getDBSession().releaseConnection(con);
}

DBSession.class
Java代码

/**
* Releases preparedStatement from the database.
* @param statement
*/
public void releaseStatement(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
dbSessionLogger.warn("Error:DBSession.releaseStatement", e);
}
}
}

***WrappedStatement.class(implements PreparedStatement)
Java代码

public void close()
throws SQLException
{
SsaGenericApplication.logDebug(this.name + "--[close]:: closing statemt...");
if ((!this.inTrans) || (!this.updateQuery))
{
this.stmt.close();
this.stmt = null;
}
SsaGenericApplication.logDebug(this.name + "--[close]::statment closed");
}

在最后这个class中就报异常了,信息如下:
10:29:10,414 ERROR [STDERR] Caused by: java.lang.NullPointerException
10:29:10,414 ERROR [STDERR] at com.***.service.baseobjects.***WrappedStatement.close(Unknown Source)
10:29:10,414 ERROR [STDERR] at com.agileitp.forte.genericdbms.DBSession.releaseStatement(DBSession.java:202)
10:29:10,414 ERROR [STDERR] at com.***.service.utils.DBUtils.release(DBUtils.java:36)
10:29:10,414 ERROR [STDERR] at com.***.service.dcustomize.TransferP1S1.processStep(TransferP1S1.java:297)
10:29:10,414 ERROR [STDERR] at com.***.service.exeprocessmanager.TransactionService.executeProcedure(Unknown Source)
10:29:10,414 ERROR [STDERR] at com.***.service.exeprocessmanager.TransactionServiceSOBean.executeProcedure(Unknown Source)
10:29:10,414 ERROR [STDERR] at sun.reflect.GeneratedMethodAccessor132.invoke(Unknown Source)
10:29:10,414 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
10:29:10,414 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:592)
10:29:10,414 ERROR [STDERR] at org.jboss.invocation.Invocation.performCall(Invocation.java:359)



理论上,最后一个class的

this.stmt.close();


不可能报错,因为之前已经做过ps!=null的判断了,至此失去思路。

不知错误可能出在哪里?
...全文
196 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
laodaoke 2012-11-29
  • 打赏
  • 举报
回复
引用 2 楼 yanning1314 的回复:
Java code?1234567891011121314public void close() throws SQLException { SsaGenericApplication.logDebug(this.name + "--[close]:: closing statemt..."); if (……
问题是,这个类是一个框架写的类,而在这之前,已经在DBSession类中进行了: if (statement != null) 的判断。 理论上到这里就肯定不是null了。
laodaoke 2012-11-29
  • 打赏
  • 举报
回复
引用 1 楼 qinzi_2013 的回复:
哪行报错?
this.stmt.close();这一行
yn00 2012-11-29
  • 打赏
  • 举报
回复

public void close()  
        throws SQLException  
      {  
        SsaGenericApplication.logDebug(this.name + "--[close]:: closing statemt...");  
        if ((!this.inTrans) || (!this.updateQuery))  
        {  
          if(this.stmt!=null){
            this.stmt.close();  
            this.stmt = null;  
          }
        }  
        SsaGenericApplication.logDebug(this.name + "--[close]::statment closed");  
      }  
加上红色部分代码
qinzi_2013 2012-11-29
  • 打赏
  • 举报
回复
哪行报错?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧