0

Trying to use setText method to set data to TextField. I want to

  1. Add a Flag to a Map.
  2. Then window appears and I enter data to TextFields.
  3. On flag pressed this window shows up with filled data in TextFields, but the problem is then I trying to press on a flag to get info, the NullPointerException 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();
    });
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Boxify
  • 23
  • 4
  • 1
    `FXMLLoader` doesn't seem to inject the `TextField`, at least not to the instance of `InfoController` you're using to call `setCountry`. Unfortunately there is not enough info to determine, if the calling code or the fxml contains the issue... – fabian May 07 '18 at 14:53
  • I'm creating a class in Controller like this: private InfoController info = new InfoController(); and is it in next lines: info.setCountry.. – Boxify May 07 '18 at 14:57
  • Creating an instance of a controller does not mean it's used when loading a fxml. Take a look here for ways to pass info to a controller properly: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian May 07 '18 at 14:59
  • Then `info` is not a controller (it is just an object that happens to be the same class as your controller); so the `@FXML`-fields will not be initialized in it. You need to get the controller from the `FXMLLoader` when you load the FXML file. See https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml. – James_D May 07 '18 at 14:59
  • Thanks guys! It worked by passing parameters.. – Boxify May 07 '18 at 15:32

0 Answers0