if 语句中的 NullPointerException

NullPointerException inside if statement

最近我一直在研究计算器,使用 java 和 javafx。我一直在为我的事件创造条件,但我收到 NullPointerException。现在这里是值:

Boolean eventTest;
String stringX, stringY;

当我 运行 this.

时,stringX 已经被定义了

使用调试器后,它归结为这行代码:

if(eventTest == false & stringY == null & stringX != null||eventTest == false & stringY != null & stringX != null)

这是堆栈跟踪:

java.lang.NullPointerException
at calculator.handle(calculator.java:263)
at calculator.handle(calculator.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access00(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null1(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

问题是为什么我会从这行代码中得到 NullPointerException,我该如何解决它?

谢谢。

检查 eventTest 是否为 null。由于 Boolean 允许 null 个值。

您的 Boolean 带有大写字母,是一个包装器 class,变量的默认值为 null。你可能只需要一个原始的 boolean。如果有特殊原因需要包装器,请对其进行初始化。

这里

 eventTest == false // default value of a Java object is null

eventTestBoolean 对象类型,它的默认值是 null.

所以 eventTestnull 并且无法将 null 拆箱到 boolean 所以你会得到 NullPointerException.

Java Language Specification 可能会帮助您理解这里发生的事情。

这里从类型 Boolean 到类型 boolean 拆箱发生,如果 r 是类型 Boolean 的引用,那么拆箱转换将 r 转换为 r.booleanValue()

rnull时。你会得到 NullPointerException.

这行代码吓到我了:

if(eventTest == false 
  & stringY == null & stringX != null ||
  eventTest == false & stringY != null & stringX != null)

您是否意识到在 || 的两种情况下您都进行相同的验证?

总之,这不是重点

eventTest 是一个 Boolean,所以它是一个对象,这意味着如果没有初始化它是空的,所以如果在检查之前没有验证它可以抛出一个 NPE对于 false/true 值。

eventTest == false 将导致 eventTest 的 unboxing ,如果它是 null ,你 获取 NullPointer 异常。

如果使用对象的值进行比较,使用equals 方法比使用== 更好。以下代码可以解决您的问题:

if((eventTest != null && eventTest == false) & stringY == null & stringX != null || (eventTest != null && eventTest == false) & stringY != null & stringX != null)

eventTest == false 在检查 null eventTest != null.

后最终可以写成 bol.equals(false)bol.booleanValue() == false

如果您不相信上面的代码,请将 eventTest 声明为原始布尔值:
boolean eventTest;