JavaFX java.lang.NullPointerException:加载 .fxml 时需要位置问题不是 getClassloader

问题描述

对于 Uni,我必须创建一个带有 GUI 的程序。 在创建了所有的逻辑和 fxml 文档之后,我尝试运行该程序,但每次都遇到同样的问题,似乎 jfx 无法加载 fxml 文档。我还没有通过 google/Stackoverflow 找到我的问题的任何解决方案,最常见的问题是他们忘记了 .getClassloader() 这不是我的问题。 当运行 mainclass 本身时,jfx 模块不会被加载,作为一种解决方法,我使用另一个 Main 调用真正的 main。

运行 workaroundmain 时出现以下异常:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at application.GUI.Main.mainWindow(Main.java:38)
    at application.GUI.Main.start(Main.java:31)

抛出异常的代码(FXMLloader.load(MainWindow)):

    private static Stage primaryStage;
    private static URL MainWindow,TableView,ChoiceWindow;
    @Override
    public void start(Stage primaryStage)
    
{
    MainWindow = getClass().getClassLoader().getResource("MainWindow.fxml");
    TableView = getClass().getClassLoader().getResource("PersonView.fxml");
    ChoiceWindow = getClass().getClassLoader().getResource("Ludger.fxml");
    Main.primaryStage = primaryStage;
    mainWindow();
    
}
public void mainWindow()
{

    try {
        Parent parent = FXMLLoader.load(MainWindow);
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (IOException e) {
        e.printstacktrace();
    }
}

我试过: 删除/添加文件重命名 MainWindow.fxml,将 MainWindow.fxml 与整个路径交换,可能还有更多没有改变任何东西。 在这一点上,我不知道要尝试解决什么问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)