FXMLLoader getController() 返回 null

FXMLLoader getController() returning null

所以我是 JavaFx 和一般编程的新手,我遇到了一个问题,我无法将 fxml 文件控制器放入我的程序中。这是代码

@FXML
private void handleBtnAdicionarAction(ActionEvent event) throws IOException {   
    Stage adicionarStage = new Stage();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/AdicionarWindow.fxml"));
    Parent root = (Parent) loader.load();
    Scene scene = new Scene(root);
    adicionarStage.setScene(scene);
    adicionarStage.centerOnScreen();
    adicionarStage.show();
    awUI = loader.getController();
    System.out.println(awUI);
    awUI.associarController(this);
}

在我执行此操作后,awUI 字段应包含我正在加载的文件的控制器,但结果为空...我一直在做其他项目,在这些项目中我执行此方法并且它一直在工作,但我不知道'不知道这里可能出了什么问题。有人可以帮我弄这个吗 ?

如果您使用的是 IntelliJ,请通过右键单击 resource 文件夹,选择 Mark Directory as -> Resources Root 来标记您的资源目录。在您的 resources 文件夹中应该有一个包含您的 AdicionarWindow.fxml 文件的 fxml。 如果不使用 Intellij,请检查您的 IDE 如何与 Resources 文件夹一起使用。

有关 intellij 的更多信息,请单击 here