没有StackTrace的Java中的NullPointerException

本文翻译自:NullPointerException in Java with no StackTrace

I've had instances of our Java code catch a NullPointerException , but when I try to log the StackTrace (which basically ends up calling Throwable.printStackTrace() ), all I get is: 我的Java代码实例捕获了NullPointerException ,但是当我尝试记录StackTrace时(基本上最终调用Throwable.printStackTrace()结束),我得到的是:

java.lang.NullPointerException

Has anyone else come across this? 还有其他人遇到吗? I tried googling for "java null pointer empty stack trace" but didn't come across anything like this. 我尝试谷歌搜索“ java空指针空堆栈跟踪”,但没有遇到这样的事情。


#1楼

参考:https://stackoom.com/question/A7Kx/没有StackTrace的Java中的NullPointerException


#2楼

exception.toString does not give you the StackTrace, it only returns exception.toString没有给您StackTrace,它仅返回

a short description of this throwable. 此throwable的简短描述。 The result is the concatenation of: 结果是以下内容的串联:

 * the name of the class of this object * ": " (a colon and a space) * the result of invoking this object's getLocalizedMessage() method 

Use exception.printStackTrace instead to output the StackTrace. 请改用exception.printStackTrace输出StackTrace。


#3楼

toString() only returns the exception name and the optional message. toString()仅返回异常名称和可选消息。 I would suggest calling 我建议打电话

exception.printStackTrace()

to dump the message, or if you need the gory details: 转储邮件,或者如果您需要血腥的详细信息,请执行以下操作:

 StackTraceElement[] trace = exception.getStackTrace()

#4楼

This will output the Exception, use only to debug you should handle you exceptions better. 这将输出异常,仅用于调试您应该更好地处理异常。

import java.io.PrintWriter;
import java.io.StringWriter;
    public static String getStackTrace(Throwable t)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

#5楼

As you mentioned in a comment, you're using log4j. 正如您在评论中提到的,您正在使用log4j。 I discovered (inadvertently) a place where I had written 我(无意间)发现了我写过的地方

LOG.error(exc);

instead of the typical 而不是典型的

LOG.error("Some informative message", e);

through laziness or perhaps just not thinking about it. 通过懒惰或者也许只是不考虑它。 The unfortunate part of this is that it doesn't behave as you expect. 不幸的是,它的行为不符合您的预期。 The logger API actually takes Object as the first argument, not a string - and then it calls toString() on the argument. logger API实际上将Object作为第一个参数而不是字符串-然后在该参数上调用toString()。 So instead of getting the nice pretty stack trace, it just prints out the toString - which in the case of NPE is pretty useless. 因此,它没有打印出漂亮的堆栈跟踪,而是打印出了toString-在NPE的情况下,它几乎没有用。

Perhaps this is what you're experiencing? 也许这就是您所经历的?


#6楼

Alternate suggestion - if you're using Eclipse, you could set a breakpoint on NullPointerException itself (in the Debug perspective, go to the "Breakpoints" tab and click on the little icon that has a ! in it) 替代建议-如果您使用的是Eclipse,则可以在NullPointerException本身上设置一个断点(在Debug透视图中,转到“ Breakpoints”选项卡,然后单击带有!的小图标)

Check both the "caught" and "uncaught" options - now when you trigger the NPE, you'll immediately breakpoint and you can then step through and see how exactly it is handled and why you're not getting a stack trace. 选中“ caught”和“ uncaught”两个选项-现在,当您触发NPE时,您将立即断点,然后逐步执行,看看它的处理方式以及为什么没有得到堆栈跟踪。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值