Trying to use setText method to set data to TextField
. I want to
- Add a Flag to a Map.
- Then window appears and I enter data to
TextField
s. - On flag pressed this window shows up with filled data in
TextField
s, but the problem is then I trying to press on a flag to get info, theNullPointerException
shows up.
There can be the problem?
setText
method in opening window controller:
NullPointerException
shows up in setCountry
method first line
public class InfoController {
@FXML
private TextField idField, nameField, countryField;
@FXML
private Button saveButton;
private String company, country;
private int code;
@FXML
void onClickClear(ActionEvent event) {
nameField.setText("");
countryField.setText("");
idField.setText("");
}
@FXML
void onSaveClick(ActionEvent event) {
if(parseInputs()) {
Information.setCode(code);
Information.setCompany(company);
Information.setCountry(country);
nameField.setText("");
countryField.setText("");
idField.setText("");
}
Stage stage = (Stage) saveButton.getScene().getWindow();
stage.close();
}
private boolean parseInputs() {
try {
company = nameField.getText();
country = countryField.getText();
code = Integer.parseInt(idField.getText());
if(nameField.getText().isEmpty() || countryField.getText().isEmpty() || idField.getText().isEmpty()) return false;
}
catch (Exception e) {
System.out.println("Unable to save information");
return false;
}
return true;
}
public void setCountry(String data) {
countryField.setText(data);
countryField.setEditable(false);
}
Place, where trying to setText before opening window.
@FXML
void addFlag(ActionEvent event) {
addingFlag.pinFlag(imageView, anchPane);
flagList = addingFlag.listOfFlags();
Button flg = flagList.get(0);
flg.setOnMouseClicked(eve -> {
String getCode = Integer.toString(Information.getCode());
info.setCountry(Information.getCountry());
info.setCode(getCode);
info.setCompany(Information.getCompany());
addingFlag.openInformationWindow();
});
}