我有以下 if 语句, where parent
, grandparent
, andchild
都是对象。出于某种原因,这个 if 在parent.isRightChild()
检查时会抛出 NullPointerError。如果这是isRightChild()
函数的问题,我会很容易抓住它,但事实并非如此。这让我感到困惑,因为应该没有办法为parent
空。提前致谢。
if (parent != null && grandparent != null
&& (parent.isLeftChild() && child.isRightChild())
|| (parent.isRightChild() && child.isLeftChild())
)
当父对象和子对象为 null 并且条件尝试访问 parent.isRightChild() 方法时。所以它抛出了 NullPointerException。
if (parent != null && grandparent != null
&& (
(parent.isLeftChild() && child.isRightChild())
|| (parent.isRightChild() && child.isLeftChild())
)
)
将方法访问条件分组为单独的(如上所示),因此如果对象为空,它将不会达到这些条件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句