【发布时间】:2014-02-05 03:29:39
【问题描述】:
我有一个 jdbc 连接,我试图关闭它,但它返回 NullPointerException。
finally
{
try
{
if(load.dbConnection!=null)
{
load.dbConnection.close();
LOGGER.info("Connection closed successfully");
}
}
catch(Exception e)
{
LOGGER.info("Exception is closing db connection"+e.getLocalizedMessage()+" "+e.getMessage()+" "+e.toString());
}
}
堆栈跟踪如图:
Exception occured nullLevel [0] - File Name: 'loadData.java' Method Name: 'verifyFiles' Line Number: '896' Message: 'java.lang.Exception'
Level [1] - File Name: 'loadData.java' Method Name: '<init>' Line Number: '106' Message: 'java.lang.Exception'
Level [2] - File Name: 'loadData.java' Method Name: 'main' Line Number: '119' Message: 'java.lang.Exception'
我在上面的代码中检查 null 条件,尽管为什么控件进入块并试图关闭连接并给我一个 java.lang.NullPointerException。
//Initial assignment
loadData load = null;
try
{
load = new loadData(); //Here the constructor calls 3 functions and exception occurs in one of those functions.
}
catch(Exception e){....}
finally
{
//The finally code shown above
}
所以在上述情况下,当构造函数中发生异常时,负载将保持为空,如第一次赋值所示。
【问题讨论】:
-
请发布堆栈跟踪并指定目标行。
-
load是null(或LOGGER是) -
load 可能为 null...LOGGER 可能为 null...
-
@Brian,@Leo :我会检查其他条件。
-
我想我听到 Spring 的 JdbcTemplate 呼唤你的名字,Dark Matter。