我正在尝试在用户首次运行应用程序时显示一个对话框。
我已将setOnShowing
设置为调用启动对话框的showFirstRunDialog
方法。当我运行应用程序时,我必须测试变量,如果它是第一次运行是真的,我得到一个例外。
错误:
Exception in Application start method
[Debug] Writing files
[Debug] writeLogs is set to false, debug logs will not be written
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:945)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
at Application.Main$1.handle(Main.java:34)
at Application.Main$1.handle(Main.java:28)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
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.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.stage.Window.fireEvent(Window.java:1380)
at javafx.graphics/javafx.stage.Window$12.invalidated(Window.java:1070)
at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
at javafx.graphics/javafx.stage.Window.setShowing(Window.java:1186)
at javafx.graphics/javafx.stage.Window.show(Window.java:1201)
at javafx.graphics/javafx.stage.Stage.show(Stage.java:276)
at Application.Main.start(Main.java:37)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
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:175)
... 1 more
Exception running application Application.Main
这是我的主要方法:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root;
System.out.println(Settings.getInstance().isFirstRun());
root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
primaryStage.setTitle("Volunteer Logger");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.setOnShowing(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
if (Settings.getInstance().isFirstRun()) {
MainWindowController controller;
FXMLLoader loader = new FXMLLoader();
controller = loader.getController();
controller.showFirstRunDialog();
}
}
});
primaryStage.show();
}
@Override
public void init() throws Exception {
//Settings.getInstance().setWriteLogs(true);
super.init();
Data.getInstance().loadFiles();
}
@Override
public void stop() throws Exception {
super.stop();
Data.getInstance().writeFiles();
}
public static void main(String[] args) {
launch(args);
}
}
谢谢!