无法确定什么是抛出NullPointer异常 [英] Can't determine what is throwing a NullPointer exception

查看:138
本文介绍了无法确定什么是抛出NullPointer异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的方法:

  try {
doStuff();
} catch(Exception ex){
logger.error(ex);
}

(我真的不使用像doStuff这样的方法名称 - 这只是为了使事情变得容易)



在doStuff中我做了各种各样的事情,其中​​包括调用数据访问方法(所以doStuff中的另一种方法)结束如下:

 } catch(SQLException ex){
logger.error(ex);
} finally {
try {
connection.close();
proc.close();
results.close();
} catch(Exception e){
logger.error(e);
} //< - 这里抛出异常。咦?
}
返回东西;

当逐步通过这段代码时,我得到第二个最后一个大括号(标有注释)和然后跳到第一个代码块中的catch,并使用NullPointer异常。 results.close()是正在运行的(结果不为空)。我的IDE(NetBeans)不提供堆栈跟踪(它显示堆栈跟踪为空)或除异常名称之外的任何其他信息(从我可以告诉的)。



此代码以前运行正常。事实上,当它运行时,我改变了存储过程,数据访问方法(我得到这个异常)正在调用,然后这个错误开始发生(没有应用程序已经停止)。我已经尝试重建和重新启动,但无济于事。我可以改回sproc,但是我真的想知道这个错误是什么,因为sproc甚至会考虑在代码中出现异常的地方,这是没有意义的。

$ b $你的doStuff()方法抛出了一个SQLException以外的东西,并没有被抓住。添加一个catch(异常e)块并记录该异常,看看会发生什么。



此代码示例展示了您所描述的相同行为:

  public class TryCatchTest {
public static void main(String [] args){
try {
System.out。的println( 富);
throw new NullPointerException();
} finally {
try {
System.out.println(bar);
} catch(Exception e){
e.printStackTrace();
}
} //这里抛出的异常
}
}


I have a method that looks like this:

try {
  doStuff();
} catch (Exception ex) {
  logger.error(ex);
}

(I don't really use method names like doStuff - this is just to make things easy)

In doStuff I do a variety of things, among them is call a data access method (so, another method within doStuff) that ends with the following:

} catch (SQLException ex) {
  logger.error(ex);
} finally {
  try {
    connection.close();
    proc.close();
    results.close();
  } catch (Exception e) {
    logger.error(e);
  } //<--Exception thrown here. HUH?
}
return stuff;

When stepping through this code I get to the second to last curly brace (marked with a comment) and then jump up to the catch in the first code block with a NullPointer exception. The results.close() is what is being run right before it (results is not null). My IDE (NetBeans) doesn't provide a stack trace (it shows the stack trace is null) or any other information other than the name of the exception (from what I can tell).

This code was running fine previously. In fact while it was running, I changed the stored procedure that the data access method (where I'm getting this exception) was calling, and then this error started to occur (without the application having been stopped at all). I've since tried rebuilding and restarting but to no avail. I could change the sproc back but I really want to find out what this error is from since it makes no sense that the sproc would even be a part of this considering where in the code the exception is occurring.

解决方案

your doStuff() method is throwing something other than a SQLException and it is not being caught. add a catch(Exception e) block and log that exception and see what happens.

this code sample exhibits the same behaviour you are describing:

public class TryCatchTest {
    public static void main(String[] args) {
        try {
            System.out.println("foo");
            throw new NullPointerException();
        } finally {
            try {
                System.out.println("bar");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } // exception thrown here
    }
}

这篇关于无法确定什么是抛出NullPointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆