没有 StackTrace 的 Java 中的 NullPointerException [英] NullPointerException in Java with no StackTrace

查看:22
本文介绍了没有 StackTrace 的 Java 中的 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Java 代码实例捕获了 NullPointerException,但是当我尝试记录 StackTrace(基本上最终调用 Throwable.printStackTrace() 时),我得到的是:

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.lang.NullPointerException

有没有其他人遇到过这个?我尝试在谷歌上搜索java 空指针空堆栈跟踪",但没有遇到这样的事情.

Has anyone else come across this? I tried googling for "java null pointer empty stack trace" but didn't come across anything like this.

推荐答案

您可能正在使用 HotSpot JVM(最初由 Sun Microsystems,后来被 Oracle 收购,是 OpenJDK 的一部分),它执行了很多优化.要返回堆栈跟踪,您需要将选项 -XX:-OmitStackTraceInFastThrow 传递给 JVM.

You are probably using the HotSpot JVM (originally by Sun Microsystems, later bought by Oracle, part of the OpenJDK), which performs a lot of optimization. To get the stack traces back, you need to pass the option -XX:-OmitStackTraceInFastThrow to the JVM.

优化是当第一次发生异常(通常是 NullPointerException)时,会打印完整的堆栈跟踪,JVM 会记住堆栈跟踪(或者可能只是代码的位置).当该异常发生的频率足够高时,不再打印堆栈跟踪,以实现更好的性能并且不会用相同的堆栈跟踪淹没日志.

The optimization is that when an exception (typically a NullPointerException) occurs for the first time, the full stack trace is printed and the JVM remembers the stack trace (or maybe just the location of the code). When that exception occurs often enough, the stack trace is not printed anymore, both to achieve better performance and not to flood the log with identical stack traces.

要了解这是如何在 HotSpot JVM 中实现的,获取一份副本 并搜索全局变量 OmitStackTraceInFastThrow.上次看代码(2019年),在文件graphKit.cpp.

To see how this is implemented in the HotSpot JVM, grab a copy of it and search for the global variable OmitStackTraceInFastThrow. Last time I looked at the code (in 2019), it was in the file graphKit.cpp.

这篇关于没有 StackTrace 的 Java 中的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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