单击按钮时获取NullPointerException(JavaFX和FXML)

时间:2018-01-01 13:07:32

标签: java user-interface javafx fxml controllers

在我点击指定按钮后最终让gui运行没有问题时,IDE(Eclipse)最终会对此错误进行长按按。

Caused by: java.lang.NullPointerException
    at ui.IOTabController.handleSourceFolderPathSubmit(IOTabController.java:32)
    ... 62 more

这是IOTab.fxml(相关部分):

<AnchorPane fx:id="IOTab" minHeight="0.0" minWidth="0.0"
    prefHeight="282.0" prefWidth="676.0" xmlns="http://javafx.com/javafx/9"
    xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.IOTabController">
    <children>
        <Pane layoutX="12.0" layoutY="50.0" prefHeight="37.0" prefWidth="625.0">
            <children>
                <TextField id="sourceFolderFullPath" layoutX="167.0" layoutY="1.0" prefHeight="26.0"
                    prefWidth="391.0"/>
                <Button id="sourceFolderPathSubmit" layoutX="567.0" mnemonicParsing="false" text="Enter" 
                onAction="#handleSourceFolderPathSubmit"/>
                <Text layoutY="18.0" strokeType="OUTSIDE" strokeWidth="0.0"
                    text="Source folder's full path:" />
            </children>
        </Pane>
        ...
</AnchorPane>

其控制器IOTabController.java:

public class IOTabController implements Initializable{  // IOTab.fxml controller

    @FXML private MainController Main;
    // variables from fxml file to inject.
    @FXML private AnchorPane IOTab;
    @FXML private TextField sourceFolderFullPath;
    @FXML private Button sourceFolderPathSubmit;
    @FXML private TextField csvFileName;
    @FXML private Button csvFileNameSubmit;
    @FXML private Button deleteExistingData;
    @FXML private Button convertCsvToKml;

    @FXML
    protected void handleSourceFolderPathSubmit(ActionEvent event) { // handles path submit.
        if (sourceFolderFullPath.getText().isEmpty()) { // if empty field
            AlertHelper.showAlert(Alert.AlertType.ERROR, "Form Error!", "Please enter path.");
            return;
        }
        AlertHelper.showAlert(Alert.AlertType.CONFIRMATION, "SUCCESS","Path inserted.");
    }

    // class for creating alerts.
    public static class AlertHelper {
        public static void showAlert(Alert.AlertType alertType, String title, String message) {
            Alert alert = new Alert(alertType);
            alert.setTitle(title);
            alert.setHeaderText(null);
            alert.setContentText(message);
            alert.showAndWait();
        }
    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

    }

}

主控制器:

public class MainController { // main.fxml controller.

    @FXML private IOTabController IOTabController;

    @FXML public void initialize() {
        System.out.println("Application started");
        IOTabController.initialize(null, null);
    }
}

gui运行正常,直到我点击“sourceFolderPathSubmit”按钮,每次点击都会弹出警告(无论我是否将文本放入TextField)。我似乎无法找到添加或删除的内容以使按钮工作。

0 个答案:

没有答案