关闭statement时出现NullPointerException
公司的一个系统出了个很奇怪的问题,方法调用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的判断了,至此失去思路。
不知错误可能出在哪里? --------------------编程问答-------------------- 哪行报错?
--------------------编程问答--------------------
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");
}
加上红色部分代码 --------------------编程问答--------------------
this.stmt.close();这一行 --------------------编程问答--------------------
问题是,这个类是一个框架写的类,而在这之前,已经在DBSession类中进行了:
if (statement != null)
的判断。
理论上到这里就肯定不是null了。
补充:Java , Web 开发