0

In my JavaFX app, I'm getting a NullPointerException I can't figure out for the life of me. The app is a simple pseudo-database which displays in this case Developer entries in a table with their various data, and the user can open a secondary window to provide some input and thus create a new entry. Code of the main window controller:

public class MainWindowController {
/*
*/
    @FXML
    public static Text textMessageDisplay;

    @FXML
    private void handleCreateDeveloper() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("CreateDeveloperWindow.fxml"));
        Scene CreateDeveloperScene;
        CreateDeveloperScene = new Scene(loader.load());
        Stage inputStage = new Stage();
        inputStage.initOwner(theStage);
        inputStage.setScene(CreateDeveloperScene);
        inputStage.showAndWait();
    }

    public void initialize() {
        developerNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
    }
/*
*/
}

Code of the secondary window where the user inputs the data for a new Developer:

public class CreateDeveloperWindowController {

    @FXML
    private Button newDeveloperCreateButton;

    @FXML
    //TODO Check for same name
    private void handleCreateDeveloperButton() {
        String proposedNewDevName = newDeveloperNameTextField.getText();
        String proposedNewDevPass = newDeveloperPassTextField.getText();
        if (proposedNewDevName.equals("") || proposedNewDevPass.equals("")) {
            MainWindowController.textMessageDisplay.setText("Name and password must not be empty!");
        } else {
            allDevelopers.add(new Developer(proposedNewDevName, proposedNewDevPass));
        }

    }
}

What this does is throw a NullPointerException at the line

MainWindowController.textMessageDisplay.setText("Name and password must not be empty!");

as soon as the Create button is pressed with the input text fields empty. Why? Everything is fine by me, textMessageDisplay is declared in MainWindowControlled and initialized from the FXML file, I can see it with its default text in the app window. So what's going on, what am I missing here?

fabian
  • 80,457
  • 12
  • 86
  • 114
Sargon1
  • 854
  • 5
  • 17
  • 48
  • I already said it seems to me that I did the declaration and initialization correctly, that the variable indeed points to an existing object. Could you perhaps elaborate on what exactly is wrong in my code? – Sargon1 Apr 07 '16 at 07:44
  • See http://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields for why you get the exception and http://stackoverflow.com/questions/34118025/javafx-pass-values-from-child-to-parent for ways to do what you're trying to do. – James_D Apr 07 '16 at 11:08

1 Answers1

0

I guess you are using textMessageDisplay statically that creates the problem, Try to accees by creating the object of MainWindowController.

DeepInJava
  • 1,871
  • 3
  • 16
  • 31