如何修复NullPointerException以在JavaFX中填充控制器?

时间:2019-02-04 22:14:44

标签: javafx javafx-8

在JavaFX中构建应用程序,使用表格填充客户名称和ID,然后添加一个按钮,一旦从表格中选择了客户名称,该按钮将启动另一个表单。我遇到了错误。

我试图更改名称populateCustomerAllName,试图更改返回类型

 @FXML 
public void handleAddButton() {
    if(customerAllTable.getSelectionModel().getSelectedItem() != null) {
        selectedCustomer = customerAllTable.getSelectionModel().getSelectedItem();
    } else {
        return;
    }
    Dialog<ButtonType> dialog = new Dialog();
    dialog.initOwner(appointmentMain.getScene().getWindow());
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("AppointmentAdd.fxml"));
    try {
        dialog.getDialogPane().setContent(fxmlLoader.load());
    } catch(IOException e) {
        System.out.println("AppointmentAdd Error: " + e.getMessage());
    }
    ButtonType save = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE);
    dialog.getDialogPane().getButtonTypes().add(save);
    dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
    AppointmentAddController controller = fxmlLoader.getController();


//error with populating .getCustomerAllName         controller.populateCustomerAllName(selectedCustomer.getCustomerAllName());
    dialog.showAndWait().ifPresent((response -> {
        if(response == save) {
            if(controller.handleAddAppointment(selectedCustomer.getCustomerAllId())) {
                monthAptTable.setItems(AppointmentDB.getMonthlyAppointments(selectedCustomer.getCustomerAllId()));
                weekAptTable.setItems(AppointmentDB.getWeeklyAppoinments(selectedCustomer.getCustomerAllId()));
            } else {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("Error");
                alert.setHeaderText("Add Appointment Error");
                alert.setContentText(controller.displayErrors());
                alert.showAndWait().ifPresent((response2 -> {
                    if(response2 == ButtonType.OK) {
                        handleAddButton();
                    }
                }));
            }
        }
    }));







public void populateCustomerAllName(String name) {
    customerAllName.setText(name);
}







Caused by: java.lang.NullPointerException
at   views.AppointmentAddController.populateCustomerAllName(AppointmentAddController.java:170)
at views.AppointmentMainController.handleAddButton(AppointmentMainController.java:155)

预期的结果是,当单击addAppointment按钮时,客户名称将填充到客户字段中。

1 个答案:

答案 0 :(得分:0)

需要更新fxml标签fx:id =“ customerAllName”