0

I have the following code:

public class Main extends Application {

    @FXML
    private Button button;

    @Override
    public void start(Stage primaryStage) {
        try {
            ...

            primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

                @Override
                public void handle(WindowEvent event) {
                    System.out.println(button)
                }

            });

            ...
    }

    @FXML
    private void initialize() {
        System.out.println(button);
    }

}

I run it, then click on the close button, and this is the output:

Button[id=button, styleClass=button]'button'
null

Why is button null when called inside setOnCloseRequest?

Arnett Rufino
  • 444
  • 4
  • 9
  • `@FXML`-annotated fields are initialized in the controller. `start(..)` is not called on the controller, so `button` is null on the instance on which `start(...)` is called. – James_D Mar 03 '17 at 13:46
  • That's right! I've managed to solve the problem by using the proper instance. Thanks a lot, @James_D! – Arnett Rufino Mar 03 '17 at 17:11

0 Answers0