切换场景时JavaFX选择框空指针异常

Posted

技术标签:

【中文标题】切换场景时JavaFX选择框空指针异常【英文标题】:JavaFX choice box null pointer exception while switching scene 【发布时间】:2022-01-04 03:21:54 【问题描述】:

该程序基本上显示选择框和一个按钮,无论用户选择什么,它都会在场景 2 的标签中打印出来。但是这个错误不断出现。

当我从选择框菜单中选择一个项目时会发生错误,而当我按下按钮时会发生另一个错误。

注意:我对两个 fxml 文件使用相同的 Controller 类。

错误:


javafx.fxml.LoadException: 
/C:/Users/yasse/eclipse-workspace/FinalProject/bin/application/exscene2.fxml

   at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
   at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
   at application.BookController.switchscene(BookController.java:57)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
   at javafx.controls/javafx.scene.control.ChoiceBox$4.invalidated(ChoiceBox.java:333)
   at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
   at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
   at javafx.controls/javafx.scene.control.ChoiceBox.setValue(ChoiceBox.java:342)
   at javafx.controls/javafx.scene.control.ChoiceBox.lambda$new$1(ChoiceBox.java:194)
   at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
   at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
   at javafx.base/javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:80)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
   at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
   at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
   at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ChoiceBox.getItems()" because "this.box" is null
   at application.BookController.initialize(BookController.java:44)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
   ... 79 more
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.label" is null
   at application.BookController.switchscene(BookController.java:66)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
   at javafx.controls/javafx.scene.control.ChoiceBox$4.invalidated(ChoiceBox.java:333)
   at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
   at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
   at javafx.controls/javafx.scene.control.ChoiceBox.setValue(ChoiceBox.java:342)
   at javafx.controls/javafx.scene.control.ChoiceBox.lambda$new$1(ChoiceBox.java:194)
   at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
   at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
   at javafx.base/javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:80)
   at javafx.base/javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
   at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
   at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
   at javafx.controls/javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:105)
   at javafx.controls/javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:223)
   at javafx.controls/javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
   at javafx.controls/javafx.scene.control.ChoiceBox$ChoiceBoxSelectionModel.select(ChoiceBox.java:586)
   at javafx.controls/javafx.scene.control.skin.ChoiceBoxSkin.lambda$addPopupItem$7(ChoiceBoxSkin.java:390)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.controls/javafx.scene.control.MenuItem.fire(MenuItem.java:459)
   at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1385)
   at javafx.controls/com.sun.javafx.scene.control.ContextMenuContent$MenuItemContainer.lambda$createChildren$12(ContextMenuContent.java:1338)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
   at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
   at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
   at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
   at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
   at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
   at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
   at java.base/java.lang.Thread.run(Thread.java:831)
javafx.fxml.LoadException: 
/C:/Users/yasse/eclipse-workspace/FinalProject/bin/application/exscene2.fxml

   at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
   at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
   at application.BookController.switchscene(BookController.java:57)
   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.base/java.lang.reflect.Method.invoke(Method.java:567)
   at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
   at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.base/java.lang.reflect.Method.invoke(Method.java:567)
   at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
   at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
   at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
   at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
   at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
   at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
   at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
   at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
   at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
   at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
   at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
   at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
   at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
   at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ChoiceBox.getItems()" because "this.box" is null
   at application.BookController.initialize(BookController.java:44)
   at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
   ... 65 more
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
   at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
   at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
   at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
   at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
   at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
   at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
   at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
   at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
   at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
   at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
   at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
   at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
   at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
   at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
   at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
   at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
   at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
   at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
   at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
   at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.reflect.InvocationTargetException
   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.base/java.lang.reflect.Method.invoke(Method.java:567)
   at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
   at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.base/java.lang.reflect.Method.invoke(Method.java:567)
   at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
   at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
   at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
   ... 46 more
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "this.label" is null
   at application.BookController.switchscene(BookController.java:66)
   ... 57 more


我的主要:

package application;    
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;



public class Main extends Application 
    //@Override
    public void start( Stage primaryStage) throws Exception 
        // TODO Auto-generated method stub
        
        AnchorPane mainpane= (AnchorPane) FXMLLoader.load(Main.class.getResource("exscene1.fxml"));
        primaryStage.setScene(new Scene(mainpane));
        
        primaryStage.show();
        
        
        
    
    
    public static void main(String[] args) 
        Application.launch(args);
    



我的控制器类:

package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

public class BookController implements Initializable 

    @FXML
    private Button button;

    @FXML
    private AnchorPane scene1pane;

    @FXML
    private ChoiceBox<String> box;

    @FXML
    private AnchorPane scene2pane;

    @FXML
    private Label label;
    
    @FXML
    private Label label1;
    
    
    
    ObservableList<String> test = FXCollections.observableArrayList("AAA","BBB","CCC"); 
    
  
    public void initialize(URL arg0, ResourceBundle arg1) 
        // TODO Auto-generated method stub
        box.getItems().addAll(test);
        box.setOnAction(this::switchscene);
        
        
    
    
    
    
  public  void switchscene(ActionEvent event)  
        
        
        AnchorPane pane;
        try 
            pane = FXMLLoader.load(getClass().getResource("exscene2.fxml"));
            scene1pane.getChildren().setAll(pane);
            
         catch (IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
    
        
        String str = box.getValue();
        label.setText(str);
        
        
        
    



我的第一个场景 fxml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane fx:id="scene1pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.BookController">
   <children>
      <Button fx:id="button" layoutX="223.0" layoutY="162.0" mnemonicParsing="false" onAction="#switchscene" text="Button">
         <font>
            <Font size="36.0" />
         </font>
      </Button>
      <Label fx:id="label1" layoutX="139.0" layoutY="50.0" prefHeight="27.0" prefWidth="148.0" text="Scene 1">
         <font>
            <Font size="42.0" />
         </font>
      </Label>
      <ChoiceBox fx:id="box" layoutX="303.0" layoutY="31.0" prefWidth="150.0" />
   </children>
</AnchorPane>


我的第二个场景 fxml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane fx:id="scene2pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.BookController">
   <children>
      <Label fx:id="label" layoutX="262.0" layoutY="92.0" text="Welcome">
         <font>
            <Font size="33.0" />
         </font>
      </Label>
   </children>
</AnchorPane>




【问题讨论】:

fxml : 控制器必须是 1:1 - 控制器不能在 fxmls 之间共享 第一个场景 FXML 没有带有 fx:id=“label” 的元素,因此在加载该 FXML 时创建的控制器中 label 为空(如堆栈跟踪所述)。 当我起草答案时,这两个错误都已在 cmets 中得到解决:) 【参考方案1】:

在这个例子中有很多事情需要考虑。首先,让我解释一下您面临的错误。

首先,单击按钮或选择选择框,您将得到constructLoadException。这是因为您对两个 fxml 使用相同的 Controller 类。而且您期望两个 fxml 共享同一个 Controller 实例。情况不会如此。使用您的代码,每个 fxml 都将拥有自己的 BookController 实例。所以在fxml1控制器中,所有与fxml2相关的实例变量都会为null,反之亦然。

所以当你启动 fxml2 时,会创建一个新的 BookController 实例并调用 initialize 方法。由于 fxml2 中没有“box”节点,因此该变量将为 null,显然会抛出 NPE,从而导致加载 fxml 的异常。

NPE的第二个错误也一样,这次你期待的是fxml1控制器中的fxml2实例变量“label”。

解决方案:

有两种方法可以解决此问题。这取决于您是否要共享相同的控制器类,或者您是否可以为每个 fxml 使用单独的控制器类。

#1。使用相同的控制器类

这样,你需要做以下两个更改。

删除 exscene2 fxml 中的控制器声明。 如下更改初始化和 switchScene 方法。主要变化是我们 正在将相同的控制器实例手动设置为 fxml2 装载机。并在初始化方法中进行一些检查,以不重新添加 再次项目。

.

public void initialize(URL arg0, ResourceBundle arg1) 
    if(box.getItems().isEmpty()) 
        box.getItems().addAll(test);
        box.setOnAction(this::switchscene);
    


public void switchscene(ActionEvent event) 
    AnchorPane pane;
    try 
        // Initiate a loader for the fxml 2
        final FXMLLoader loader = new FXMLLoader(getClass().getResource("exscene2.fxml"));
        // Set this controller instance to fxml 2
        loader.setController(this);
        // And load the fxml 2.
        pane = loader.load();
        // By this line, both fxmls use same instance of controller
        scene1pane.getChildren().setAll(pane);
     catch (IOException e) 
        e.printStackTrace();
    

    String str = box.getValue();
    if(str!=null && !str.isEmpty()) 
        label.setText(str);
    

#2。使用单独的控制器类

这样,你需要做以下两个更改。

使用仅与 fxml 2 相关的实例变量为 fxml 2(比如说 BookController2.java)创建一个新的 Controller 类。

.

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

import java.net.URL;
import java.util.ResourceBundle;

public class BookController2 implements Initializable 

    @FXML
    private AnchorPane scene2pane;

    @FXML
    private Label label;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) 
        // Empty
    

    public final Label getLabel()
        return label;
    

将此 BookController2 设置为 fxml 2 中的控制器。

fx:controller="application.BookController2"

在 BookController.java 中删除 fxml 2 的所有实例变量并更改 switchScene 方法如下。

.

public void switchscene(ActionEvent event) 
    AnchorPane pane;
    try 
        // Initiate the loader for fxml 2. Using this loader, you can get the controller instance of fxml 2
        final FXMLLoader loader = new FXMLLoader(getClass().getResource("exscene2.fxml"));
        pane = loader.load();
        scene1pane.getChildren().setAll(pane);

        String str = box.getValue();
        if (str != null && !str.isEmpty()) 
            // Get the label from the controller and set the text.
            ((BookController2) loader.getController()).getLabel().setText(str);
        
     catch (IOException e) 
        e.printStackTrace();
    

【讨论】:

第一个解决方案(我不太推荐)远不止“共享同一个控制器class”。它实际上共享同一个控制器(即实际的控制器实例)。 @James_D 感谢您的更正。我的实际意图也是一样的..共享同一个控制器实例。

以上是关于切换场景时JavaFX选择框空指针异常的主要内容,如果未能解决你的问题,请参考以下文章

空指针异常数组[重复]

Android getIntent() 空指针异常

Java8新特性Optional类在处理空值判断场景的应用 回避空指针异常 编写健壮的应用程序

JAXB RI ClassFactory 中的空指针异常

并发场景下list的空指针异常和size大小问题

初始化环行缓冲区失败,报空指针异常的debug调试