javafxml错误:应用程序启动方法中出现异常

gupuwyp2  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(496)

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

如何确定我的javafx应用程序所需的fxml文件、css文件、图像和其他资源的正确路径(1个答案)
上个月关门了。
我创建了一个javafx项目,运行时出现错误,我怀疑错误在以下主类中:

import java.io.IOException;

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

    public class Main extends Application {

       private Stage primaryStage;
       private AnchorPane mainLayout;

       public void start(Stage primaryStage) throws IOException {
           this.primaryStage = primaryStage;
           this.primaryStage.setTitle("Real Estate Listings");
           showMainGUI();
       }

       private void showMainGUI() throws IOException {
           FXMLLoader loader = new FXMLLoader();
           loader.setLocation(Main.class.getResource("C:\\Users\\micha\\Documents\\eclipse-workspace\\ProgAssignment2\\src\\MainGUI.fxml"));
           mainLayout = loader.load();
           Scene scene = new Scene(mainLayout);
           primaryStage.setScene(scene);
           primaryStage.show();
       }

       public static void main(String[] args) {
           launch(args);
       }
    }

错误如下:

Exception in Application start method
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:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3316)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3280)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3249)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3222)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3199)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3192)
    at Main.showMainGUI(Main.java:22)
    at Main.start(Main.java:17)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    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:174)
    ... 1 more
Exception running application Main

经过一些研究,我发现错误很可能存在于对fxml加载程序的调用中,但是在尝试了几个不同的修复之后,我没有找到解决方案。我是否错误地创建了fxml对象?谢谢

zte4gxcn

zte4gxcn1#

我想仔细看看 NullPointerException 你发布的stacktrace中提到的部分。文件路径正确吗?

ibps3vxo

ibps3vxo2#

我以前也遇到过类似的问题。尝试更改:

loader.setLocation(Main.class.getResource("C:\\Users\\micha\\Documents\\eclipse-workspace\\ProgAssignment2\\src\\MainGUI.fxml"));

收件人:

loader.setLocation(getClass().getClassLoader().getResource("C:\\Users\\micha\\Documents\\eclipse-workspace\\ProgAssignment2\\src\\MainGUI.fxml"));

相关问题