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를 배치 한 다음 메인 메소드에서 해당 메소드를 호출, 나는에도 불구하고 null 값을 얻을 값을 다른 것으로 설정하십시오! 또한 컴파일러에서 오류가 발생하지 않으므로 어딘가에 논리적 오류라고 확신하지만 해결할 수는 없습니다. 당신은 ControllerUpdateData
의 initialize
이를 교체해야
그래서 왜 txt1.setText (controllerMain.getNameCompany()); // get Null'이 줄은 NullPointerException을 던집니다? – DVarga
System.out.println (controllerMain.getNameCompany()); // 인쇄 null –
읽지 못했습니다 ... [mcve]를 참조하십시오. –