我已经有我们的Java代码的实例捕捉一个NullPointerException,但当我试图记录StackTrace(基本上最终调用Throwable.printStackTrace()),我得到的是:
java.lang.NullPointerException
有人遇到过吗?我试着在谷歌上搜索“java空指针空堆栈跟踪”,但没有遇到这样的东西。
我已经有我们的Java代码的实例捕捉一个NullPointerException,但当我试图记录StackTrace(基本上最终调用Throwable.printStackTrace()),我得到的是:
java.lang.NullPointerException
有人遇到过吗?我试着在谷歌上搜索“java空指针空堆栈跟踪”,但没有遇到这样的东西。
当前回答
(您的问题仍然不清楚您的代码是否调用printStackTrace()或这是由日志处理程序完成的。)
以下是一些可能的解释:
The logger / handler being used has been configured to only output the exception's message string, not a full stack trace. Your application (or some third-party library) is logging the exception using LOG.error(ex); rather than the 2-argument form of (for example) the log4j Logger method. The message is coming from somewhere different to where you think it is; e.g. it is actually coming some third-party library method, or some random stuff left over from earlier attempts to debug. The exception that is being logged has overloaded some methods to obscure the stacktrace. If that is the case, the exception won't be a genuine NullPointerException, but will be some custom subtype of NPE or even some unconnected exception.
我认为最后一种可能的解释是不太可能的,但人们至少会考虑做这种事情来“防止”逆向工程。当然,它只会让诚实的开发者的生活变得困难。
其他回答
这将输出异常,只用于调试,你应该更好地处理你的异常。
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();
}
例外。toString不给你StackTrace,它只返回
这是一个简短的描述扔。 结果是: *该对象的类名 * ": "(冒号加空格) *调用该对象的getLocalizedMessage()方法的结果
使用异常。而不是printStackTrace来输出StackTrace。
(您的问题仍然不清楚您的代码是否调用printStackTrace()或这是由日志处理程序完成的。)
以下是一些可能的解释:
The logger / handler being used has been configured to only output the exception's message string, not a full stack trace. Your application (or some third-party library) is logging the exception using LOG.error(ex); rather than the 2-argument form of (for example) the log4j Logger method. The message is coming from somewhere different to where you think it is; e.g. it is actually coming some third-party library method, or some random stuff left over from earlier attempts to debug. The exception that is being logged has overloaded some methods to obscure the stacktrace. If that is the case, the exception won't be a genuine NullPointerException, but will be some custom subtype of NPE or even some unconnected exception.
我认为最后一种可能的解释是不太可能的,但人们至少会考虑做这种事情来“防止”逆向工程。当然,它只会让诚实的开发者的生活变得困难。
当您在项目中使用AspectJ时,可能会发生某些方面隐藏其堆栈跟踪的部分。例如,今天我有:
java.lang.NullPointerException:
at com.company.product.MyTest.test(MyTest.java:37)
这个堆栈跟踪是在通过Maven的surefire运行测试时打印的。
另一方面,当在IntelliJ中运行测试时,会打印不同的堆栈跟踪:
java.lang.NullPointerException
at com.company.product.library.ArgumentChecker.nonNull(ArgumentChecker.java:67)
at ...
at com.company.product.aspects.CheckArgumentsAspect.wrap(CheckArgumentsAspect.java:82)
at ...
at com.company.product.MyTest.test(MyTest.java:37)
toString()只返回异常名称和可选消息。我建议你打电话
exception.printStackTrace()
要转储消息,或者如果你需要血腥的细节:
StackTraceElement[] trace = exception.getStackTrace()