在初始化中访问FXML元素时获取NullPointerException

时间:2018-01-05 13:31:55

标签: java javafx

我在JavaFX中创建一个从.fxml文件加载的导出对话框。要初始化方法,我想使用initialize()方法。但是,在initialize()调用期间访问@FXML gui元素时,我得到NullPointerException,因为它们仍为null。

这里我的代码是在我的controller.MainController类中创建对话框:

        // Create dialog
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/view/exportDialog.fxml" ));
        AnchorPane exportDialog = (AnchorPane) loader.load();
        exportController = (ExportDialogController)loader.getController();
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Export...");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(this.stage);
        Scene scene = new Scene(exportDialog);
        dialogStage.setScene(scene);
        dialogStage.show();

这是导出对话框的控制器,它存储在controller.ExportDialogController中:

public class ExportDialogController {
    Stage stage = null;

    // GUI Elements
    @FXML
    private AnchorPane anchorPane;

    @FXML
    private ProgressBar exportProgressBar;

    @FXML
    private Label exportStatusLabel;

    @FXML
    private Button exportButton;

    public void initialize() {
        Stage stage = (Stage)exportButton.getScene().getWindow();
        stage.setOnCloseRequest(event -> close());
    }
}

以下是我的.fxml文件的代码,该文件存储在view.exportDialog中:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.ExportDialogController">
   <children>
      <ProgressBar fx:id="exportProgressBar" layoutX="75.0" layoutY="155.0" prefHeight="28.0" prefWidth="450.0" progress="0.0" AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="25.0" AnchorPane.topAnchor="50.0" />
      <Label layoutX="47.0" layoutY="42.0" prefHeight="17.0" prefWidth="218.0" text="Exporting dataset....." AnchorPane.leftAnchor="25.0" AnchorPane.topAnchor="25.0" />
      <Label fx:id="exportStatusLabel" alignment="CENTER" layoutX="286.0" layoutY="203.0" text="Export successful!" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="90.0" />
      <Button fx:id="exportButton" layoutX="261.0" layoutY="115.0" mnemonicParsing="false" onAction="#exportButtonPressed" prefHeight="25.0" prefWidth="79.0" text="Cancel" AnchorPane.bottomAnchor="25.0" AnchorPane.topAnchor="120.0" />
   </children>
</AnchorPane>

我已将.fxml文件中的控制器链接起来了,我还添加了fx:ids。

enter image description here

根据JavaFX的文档,gui元素应该已经注入了。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

在调用initialize()期间调用loader.load()方法。显然,并且必然,导出按钮不是那时的场景的一部分(在你调用new Scene(exportDialog)之前不会发生),所以exportButton.getScene()返回null并且exportButton.getScene().getWindow()抛出空指针异常。

由于该阶段是从MainController类中的代码进行管理的,因此您应该在那里注册监听器:

    // Create dialog
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/view/exportDialog.fxml" ));
    AnchorPane exportDialog = (AnchorPane) loader.load();
    exportController = (ExportDialogController)loader.getController();
    Stage dialogStage = new Stage();

    dialogStage.setOnCloseRequest(event -> exportController.close());

    dialogStage.setTitle("Export...");
    dialogStage.initModality(Modality.WINDOW_MODAL);
    dialogStage.initOwner(this.stage);
    Scene scene = new Scene(exportDialog);
    dialogStage.setScene(scene);
    dialogStage.show();

并从initialize()中的ExportDialogController方法中删除代码。