控制器的getter方法返回null

时间:2016-06-24 08:25:34

标签: java javafx

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值!另外,我没有从编译器收到任何错误,所以我确定它在某个地方是一个逻辑错误,但我无法弄明白。

1 个答案:

答案 0 :(得分:0)

您应该在initialize的{​​{1}} ...

中替换它
ControllerUpdateData

...与......

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

问题是,如果您只是使用其默认构造函数创建FXMLLoader loader = new FXMLLoader(getClass().getResource("... FXML_of_ControllerMain ...")); ControllerMain controllerMain = (ControllerMain) loader.getController(); 的实例,则ControllerMain方法永远不会被调用,因此您的类成员都不会被初始化。

您应该使用initialize加载FXML文件,FXMLLoader上的FXMLLoader将创建关联控制器类的实例并调用load方法那个例子。可以使用getController方法检索此对象。

以下是really detailed answer如何从控制器加载FXML文件。