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?