是否可以从堆栈跟踪中区分哪一部分为空?

Is it possible to distinguish which part was null from stack trace?

当我有这样的方法时:

public class Tttttt {

    public Thing getThingBy(String name) {
      return aaa.getThingBy(name);
    }
}

然后我在日志中看到 NPE

Caused by java.lang.NullPointerException
   at xxx.yyy.Tttttt.getThingBy(SourceFile:666)

并且666符合方法定义,那么我可以说namenull还是aaanull

在这种情况下 aaanull

name 也可以是 null 但它将作为参数传递给 getThingBy 并且如果抛出异常,您的堆栈跟踪中会有更多条目那里。

can I say whether name was null or aaa was null?

这是一个糟糕的例子...aaa 肯定是空的。将 null 参数传递给该方法不会引发任何异常。即使该方法确实调用了自身,您也会在堆栈跟踪中看到它。

假设它再次被调用,并且您在完全相同的行上使用了 aaa.someMethod(name.length())。那么你可能仍然能够根据stacktrace中提到的class类型和方法来确定变量,至于这个问题at xxx.yyy.Tttttt.getThingBy compared to Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference