简体   繁体   English

关闭JDBC连接时出现问题

[英]Issue in closing JDBC Connection

I have a jdbc connection which I'm trying to close but its returning a NullPointerException. 我有一个试图关闭的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());

        }
    }

The stack trace is as shown: 堆栈跟踪如下所示:

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'

I the above code I'm checking for null condition in spite of that why is the control entering the block and trying to close connection and also giving me a java.lang.NullPointerException. 我在上面的代码中我正在检查空条件,尽管如此,为什么控件进入块并试图关闭连接,并且还给了我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
    }

SO in the above case when exception occurs in the constructor will the load remain null as shown in first assignment. 因此,在上述情况下,当构造函数中发生异常时,SO将使负载保持为空,如第一次分配所示。

I could only suggest to debug your code more carefully, something like this: 我只能建议更仔细地调试代码,如下所示:

        //Initial assignment
        loadData load = null;
        System.out.println("logger is a null?: " + LOGGER);
        LOGGER.info("load: "+load);
        try
        {

            load = new loadData(); //Here the constructor calls 3 functions and exception occurs in one of those functions.
            LOGGER.info("load init: "+load);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally
        {
            LOGGER.info("finally load: "+load);
            try
            {
                LOGGER.info("finally connection: " + load.dbConnection);
                if(load.dbConnection!=null)
                {
                    load.dbConnection.close();
                    LOGGER.info("Connection closed successfully");
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM