javafx:打开新窗格时返回nullpointerexception

6yjfywim  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(424)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
上个月关门了。
我在做一个有两个屏幕的舞台的项目。一个用来推车,另一个用来结账。其思想是,单个阶段将包含一个窗格,用于为这些屏幕中的每个屏幕加载fxml文件。舞台本身打开得很好,但问题来自于试图打开单独的窗格。我正在基于单例方法构建代码,因为在任何给定的时间,这些阶段中只有一个应该打开。
下面是orderstagecontroller的代码

  1. package Controllers;
  2. import javafx.fxml.FXML;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.scene.Parent;
  5. import javafx.scene.Scene;
  6. import javafx.scene.layout.Pane;
  7. import javafx.stage.Stage;
  8. import java.io.IOException;
  9. public class OrderStageController {
  10. static OrderStageController instance = null;
  11. public Stage cartStage=new Stage();
  12. Parent root;
  13. @FXML Pane paneOrder;
  14. public static OrderStageController getInstance(){ return instance; }
  15. public void initialize() throws IOException {
  16. instance=this;
  17. openCartPane();
  18. }
  19. public void openCartStage() throws Exception{
  20. root= FXMLLoader.load(getClass().getResource("/FXML_Files/OrderStage.fxml"));
  21. Scene scene=new Scene(root,655,555);
  22. scene.getStylesheets().add(getClass().getResource("/FXML_Files/test.css").toExternalForm());
  23. scene.getStylesheets().add(getClass().getResource("/FXML_Files/login.css").toExternalForm());
  24. cartStage.setTitle("Placeholder");
  25. cartStage.setScene(scene);
  26. cartStage.show();
  27. }
  28. public void openCartPane() throws IOException {
  29. Pane paneCart=FXMLLoader.load(getClass().getResource("/FXML_Files/MyCartScreen.fxml"));
  30. paneOrder.getChildren().clear();
  31. paneOrder.getChildren().add(paneCart);
  32. }
  33. public void openCheckoutPane() throws IOException {
  34. Pane paneCheckout=FXMLLoader.load(getClass().getResource("/FXML_Files/CheckoutScreen.FXML"));
  35. paneOrder.getChildren().clear();
  36. paneOrder.getChildren().add(paneCheckout);
  37. }
  38. }

这是cartcontroller的当前空控制器。我还没有添加任何内容,因为我甚至无法打开它。我把它包括在内,以防万一有什么问题。

  1. package Controllers;
  2. public class MyCartController {
  3. // Classes
  4. OrderStageController orderStageController=OrderStageController.getInstance();
  5. }

fxml文件很好。一切都在它应该在的地方,他们自己运行良好。
当我试图打开stage并且initializer方法试图运行“opencartpane”方法时,问题就出现了。我收到一个nullpointerexception,指出orderpane是空值。
我得到的堆栈跟踪是

  1. Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
  2. at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
  3. at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
  4. at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
  5. at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
  6. at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
  7. at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
  8. at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
  9. at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
  10. at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
  11. at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
  12. at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
  13. at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
  14. at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
  15. at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
  16. at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
  17. at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8885)
  18. at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
  19. at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
  20. at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
  21. at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
  22. at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
  23. at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
  24. at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
  25. at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
  26. at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
  27. at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
  28. at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
  29. at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
  30. at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
  31. at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
  32. at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
  33. at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
  34. at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
  35. at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3890)
  36. at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1885)
  37. at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2618)
  38. at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
  39. at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
  40. at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
  41. at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
  42. at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
  43. at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
  44. at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
  45. at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
  46. at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  47. at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
  48. at java.base/java.lang.Thread.run(Thread.java:832)
  49. Caused by: java.lang.reflect.InvocationTargetException
  50. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  51. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  52. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  53. at java.base/java.lang.reflect.Method.invoke(Method.java:564)
  54. at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
  55. at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
  56. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  57. at java.base/java.lang.reflect.Method.invoke(Method.java:564)
  58. at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
  59. at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
  60. at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
  61. ... 46 more
  62. Caused by: javafx.fxml.LoadException:
  63. /C:/Users/Joshua/JAVA/Fall20_FoodOrdering/out/production/SeniorProject/FXML_Files/OrderStage.fxml
  64. at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
  65. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
  66. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
  67. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
  68. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
  69. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
  70. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
  71. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
  72. at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
  73. at Controllers.OrderStageController.openCartStage(OrderStageController.java:25)
  74. at Controllers.MainStageController.openCartPane(MainStageController.java:46)
  75. ... 57 more
  76. Caused by: java.lang.reflect.InvocationTargetException
  77. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  78. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  79. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  80. at java.base/java.lang.reflect.Method.invoke(Method.java:564)
  81. at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
  82. at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
  83. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  84. at java.base/java.lang.reflect.Method.invoke(Method.java:564)
  85. at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
  86. at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
  87. at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
  88. ... 66 more
  89. Caused by: java.lang.NullPointerException
  90. at Controllers.OrderStageController.openCartPane(OrderStageController.java:36)
  91. at Controllers.OrderStageController.initialize(OrderStageController.java:21)
  92. ... 77 more

这个错误似乎是由于线路的原因而发生的 paneOrder.getChildren().clear();openCartPane() 方法。我已经对它做了一段时间的测试,我自己也找不到原因。
任何帮助都将不胜感激!
附加
我在调试器中做了更多的挖掘,在崩溃时找到了这些变量值。

他们没有给我多少关于这个问题的线索,但也许这会对某些人有所帮助。

jxct1oxe

jxct1oxe1#

感谢@lyes的帮助。这个问题原来是控制器的fxml文件中的一个重命名变量。Git和我的健忘是罪魁祸首。

相关问题