1
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;
}}

and other class

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()); // 
}}

When I place the getters and setters in a method, then call that method in the main method, I get a value of null despite having set the value to something else! Additionally, I'm not receiving any errors from the compiler, so I'm sure it's a logical error somewhere but I cannot figure it out.

DVarga
  • 21,311
  • 6
  • 55
  • 60
D.Stifler
  • 105
  • 1
  • 1
  • 5

1 Answers1

0

You should replace this in initialize of ControllerUpdateData ...

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

... with ...

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

The problem is that if you just create an instance of ControllerMain using its default constructor, the initialize method never gets called, therefore none of your class members become initialized.

You should use the FXMLLoader to load the FXML file, and the FXMLLoader on load will create an instance of the associated controller class and calls the initialize method on that instance. This object can be retrieved with the getController method.

Here is a really detailed answer how to load FXML files from controllers.

Community
  • 1
  • 1
DVarga
  • 21,311
  • 6
  • 55
  • 60