簡體   English   中英

控制器的getter方法返回null

[英]Getter methods of controller return null

public class ControllerMain implements Initializable {
private int id;
private String nameCompany;
private int phone;
private String address;
private String other;
static ObservableList<UserData> data = FXCollections.observableArrayList();

@FXML
private Button btnAdd;
@FXML
public TableView<UserData> table = new TableView<>();
@FXML
private TableColumn<UserData, String> column1;
@FXML
private TableColumn<UserData, Integer> column2;
@FXML
private TableColumn<UserData, String> column3;
@FXML
private TableColumn<UserData, String> column4;
@FXML
private TableColumn<UserData, Integer> column5;

@Override
public void initialize(URL location, ResourceBundle resources) {
    String companyName = "companyName";
    column1.setCellValueFactory(new PropertyValueFactory<UserData, String>(companyName));
    String phone = "phone";
    column2.setCellValueFactory(new PropertyValueFactory<UserData, Integer>(phone));
    String address = "address";
    column3.setCellValueFactory(new PropertyValueFactory<UserData, String>(address));
    String other = "other";
    column4.setCellValueFactory(new PropertyValueFactory<UserData, String>(other));
    String id = "id";
    column5.setCellValueFactory(new PropertyValueFactory<UserData, Integer>(id));
    column5.setVisible(false);
    loadDatabaseData();
}

@FXML
private void openAddForm() {
    try {
        MainApp.showAddForm();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@FXML
private void deleteData() {
    try (Connection con = new DBConnect().getConnected();
         PreparedStatement prep = con.prepareStatement("DELETE FROM job.job WHERE job.id = ?")) {
        UserData selectedItem = table.getSelectionModel().getSelectedItem();
        prep.setInt(1, selectedItem.idProperty().getValue());
        prep.execute();
        data.remove(selectedItem);
    } catch (Exception e) {
        System.err.println("Ошибка удаления: " + e.getMessage());
    }
}

@FXML
private void openUpdateForm() {
    try {
        UserData selectedItem = table.getSelectionModel().getSelectedItem();
        setId(selectedItem.idProperty().getValue());
        setNameCompany(selectedItem.companyNameProperty().getValue());
        setPhone(selectedItem.phoneProperty().getValue());
        setAddress(selectedItem.addressProperty().getValue());
        setOther(selectedItem.otherProperty().getValue());
        MainApp.showUpdateForm();
    } catch (Exception e) {
        System.err.println("Ошибка открытия формы редактивания: " + e.getMessage());
        e.printStackTrace();
    }
}

void loadDatabaseData() {
    try (Connection con = new DBConnect().getConnected();
         PreparedStatement preparedStatement = con.prepareStatement("SELECT  * FROM job.job");
         ResultSet resultSet = preparedStatement.executeQuery()) {
        data.clear();
        while (resultSet.next()) {
            data.add(new UserData(
                    resultSet.getInt("id"),
                    resultSet.getString("company_name"),
                    resultSet.getInt("phone"),
                    resultSet.getString("address"),
                    resultSet.getString("other")
            ));
            table.setItems(data);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }
}


private void setId(int id) {
    this.id = id;
}

String getNameCompany() {
    return nameCompany;
}

private void setNameCompany(String nameCompany) {
    this.nameCompany = nameCompany;
}

int getPhone() {
    return phone;
}

private void setPhone(int phone) {
    this.phone = phone;
}

String getAddress() {
    return address;
}

private void setAddress(String address) {
    this.address = address;
}

String getOther() {
    return other;
}

private void setOther(String other) {
    this.other = other;
}}

和其他班級

public class ControllerUpdateData implements Initializable {
@FXML
private TextField txt2;
@FXML
private TextField txt3;
@FXML
private TextField txt4;
@FXML
private TextField txt1;

@FXML
private void updateData() {
    ControllerMain controllerMain = new ControllerMain();
    try (Connection con = new DBConnect().getConnected();
         PreparedStatement prep = con.prepareStatement("UPDATE job.job SET company_name=?,phone=?,address=?,other=? WHERE job.id=?;", Statement.RETURN_GENERATED_KEYS)) {
        prep.setString(1, txt1.getText());
        prep.setInt(2, Integer.parseInt(txt2.getText()));
        prep.setString(3, txt3.getText());
        prep.setString(4, txt4.getText());
        prep.setInt(5, 1);
        prep.execute();
        txt1.clear();
        txt2.clear();
        txt3.clear();
        txt4.clear();
        controllerMain.loadDatabaseData();
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException | SQLException e) {
        System.err.println("Error: " + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
}


@Override
public void initialize(URL location, ResourceBundle resources) {
    ControllerMain controllerMain = new ControllerMain(); // get Null
    txt1.setText(controllerMain.getNameCompany()); // get Null
    txt2.setText(String.valueOf(controllerMain.getPhone())); // get Null
    txt3.setText(controllerMain.getAddress()); // get Null
    txt4.setText(controllerMain.getOther()); // 
}}

當我在方法中放置getter和setter,然后在main方法中調用該方法時,盡管已將值設置為其他值,但我得到null值! 另外,我沒有從編譯器收到任何錯誤,所以我確定這是一個邏輯錯誤,但我無法弄明白。

您應該在initialize ControllerUpdateData替換它...

ControllerMain controllerMain = new ControllerMain(); // get Null

...... ......

FXMLLoader loader = new FXMLLoader(getClass().getResource("... FXML_of_ControllerMain ..."));
ControllerMain controllerMain = (ControllerMain) loader.getController();

問題是,如果您只是使用其默認構造函數創建ControllerMain的實例,則永遠不會調用initialize方法,因此您的類成員都不會被初始化。

您應該使用FXMLLoader加載FXML文件, loadFXMLLoader將創建關聯控制器類的實例,並在該實例上調用initialize方法。 可以使用getController方法檢索此對象。

這是一個非常詳細的答案,如何從控制器加載FXML文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM