关闭JDBC连接时出现java问题
我有一个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'
我在上面的代码中检查空条件,尽管如此,为什么控件进入块并试图关闭连接,同时给我一个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
}
因此,在上述情况下,当构造函数中发生异常时,加载将保持为null,如第一次赋值所示
# 1 楼答案
我只能建议您更仔细地调试代码,比如: