首页 > 解决方案 > JavaFX java.lang.NullPointerException:加载 .fxml 时需要位置(问题不是 getClassloader)

问题描述

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

运行 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,可能还有很多没有改变任何东西。在这一点上,我不知道要尝试什么来解决问题。

标签: javajavafxfxmlloader

解决方案


推荐阅读