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?

2
  • 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? Commented Apr 7, 2016 at 7:44
  • See stackoverflow.com/questions/23105433/… for why you get the exception and stackoverflow.com/questions/34118025/… for ways to do what you're trying to do. Commented Apr 7, 2016 at 11:08

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.