我正在努力解决一块代码在初始化之前运行的问题。
@FXML
public void Start(int pesel) throws IOException {
klientPesel = pesel;
root = FXMLLoader.load(getClass().getClassLoader().getResource("klientScene.fxml"));
Main.mainStage.setTitle("Agencja Ubezpieczniowa - Agent");
Scene scene = new Scene(root,800,600);
Main.mainStage.setScene(scene);
Main.mainStage.show();
obtainKlient();
setKlientLabels();
}
所以确切的问题是我有一个变量@FXML private VBox KlientInfoVBOX;
并且它在setKlientLabels()
运行之前没有被初始化。如何确保在setKlientLabels()
之后Main.mainStage.show();
运行?
Mabey stacktrace将有助于解决问题
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at app.KlientSearchController.setKlientLabels(KlientSearchController.java:69)
at app.KlientSearchController.Start(KlientSearchController.java:51)
at app.UserSceneController.StartKlientSearch(UserSceneController.java:87)
at app.UserSceneController.lambda$wyszukajKlienta$0(UserSceneController.java:70)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
如果我从要运行的代码中删除setKlientLabels()
,则没有例外,eveyrhting顺利运行。
答案 0 :(得分:3)
您需要设置onShowingProperty ()
的{{1}}。当舞台显示时,Stage
将被触发。要设置onShowingProperty ()
,只需使用onShowingProperty ()
方法。
setOnShowing ()
答案 1 :(得分:0)
Main.mainStage.setOnShowing()
解决方案在 JavaFX 11 中对我不起作用,但将其放入主应用程序类中:
@Override
public void start(Stage stage) {
stage.addEventFilter(WindowEvent.WINDOW_SHOWN,
new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
log.info("Stage shown and {} total windows existing",
Window.getWindows().size()");
}
}
);
}
或者这个更简单的 lambda 表达式等价物:
stage.setOnShown((event) -> {
log.info("Stage shown and {} total windows existing",
Window.getWindows().size());
});
这适用于在任何 JavaFX 阶段/窗口(包括初级阶段)之后运行代码。