关闭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的判断了,至此失去思路。
不知错误可能出在哪里?
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的判断了,至此失去思路。
不知错误可能出在哪里?
...全文
相关推荐
java开发中的一些常用小技巧
一.用JDBC时的一些注意事项 1.多使用PreparedStatement代替Statement这样可以避免在拼接字符串的时候出现 "select * from emp where name = '"+name+"'"单引号过多的情况 2 sql语句过长的时候尽量避免使用sql="str1"+"str2"; 而使用StringBuffer sql = new StringBuffer("str1"); str.append("str2")代替 3在使用数据库操作的时候尽量多用finally语句,进行资源的释放。 try{}catch(Exception e){} finally{if(conn!=null){conn.colse();conn==null}} 4事务的处理 a关闭Connection的自动提交connn.setAutoCommit; b执行每一个新的sql语句前,上一次执行sql语句的Statement(或者PreparedStatemet)必须先close ; c、提交 connn.commit; d、如果发生异常,那么回滚 conn.rollback;
java实现将结果集封装到List中的方法
主要介绍了java实现将结果集封装到List中的方法,涉及java数据库查询及结果集转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
java面试题及答案
适用于Java实习生,及初级Java工程师
经典java面试题下载
java经典面试题,大家快来下载吧!!!!!!!!!
java常见错误集合以及描述
用来解决常见的Java异常处理。大大的提高你的开发效率


Web 开发
