title: How to save FXML control states before they're discarded?
body: <p>This is my first FXML application.  Previously I've gotten pretty comfortable with Swing.  I simply want to save several values from the controls on the stage, so that user-input values reload across runs.  The <code>ReliableOneShotCloseHandler</code> is a JavaFX version of something I <a href="https://stackoverflow.com/questions/30154572/in-java-where-to-put-code-to-reliably-fire-on-window-close/31774896#31774896">posted a while ago here</a>.  However, by the time the windowClosing function is called, it seems that the controls have already been disposed of ... ? I get a NullPointerException at the indicated line:</p>
  
  <pre><code>package testfxmlloader;
  
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.application.Application;
  import javafx.event.EventHandler;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.fxml.JavaFXBuilderFactory;
  import javafx.scene.control.Label;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  import javafx.stage.WindowEvent;
  
  /**
  *
  * @author maskedcoder
  */
  public class FXMLDocumentController implements Initializable {
  
      @FXML
      private Label label;
  
      @FXML
      private AnchorPane apMain;
  
      @FXML
      private void handleButtonAction(ActionEvent event) {
          System.out.println("You clicked me!");
          label.setText("Hello World!");
      }
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
          ReliableOneShotCloseHandler roscSaveSettings = new ReliableOneShotCloseHandler(apMain.getScene().getWindow(), new ReliableOneShotCloseHandler.CloseDuties() {
  
              @Override
              public boolean confirmClose(WindowEvent e) {
                  return true;
              }
  
              @Override
              public void windowClosing(WindowEvent e) {
                  saveSettings();
              }
          });
      }   
  
      public void saveSettings() {
          System.out.println("save setting: " + label.getText());  // gets NullPointerException because label is null.
      }
  
  }
  
  class ReliableOneShotCloseHandler {
  
      public interface CloseDuties {
          public boolean confirmClose(WindowEvent e);
          public void windowClosing(WindowEvent e);
      }
  
      private final CloseDuties closeDuties;
      private boolean windowClosingFired = false;
      private final EventHandler&lt;WindowEvent&gt; openEvent;
      private final EventHandler&lt;WindowEvent&gt; closeEvent;
  
      public ReliableOneShotCloseHandler(Window thisWindow, CloseDuties iniCloseDuties) {
          super();
          closeDuties = iniCloseDuties;
          openEvent = (WindowEvent event) -&gt; {
              windowClosingFired = false;
          };
          closeEvent = (WindowEvent event) -&gt; {
              if(!windowClosingFired) {
                  if(closeDuties.confirmClose(event)) {
                      closeDuties.windowClosing(event);
                      windowClosingFired = true;
                  }
                  else
                      event.consume();
              }
          };
  
          thisWindow.setOnShowing(openEvent);
          thisWindow.setOnShown(openEvent);
          thisWindow.setOnCloseRequest(closeEvent);
          thisWindow.setOnHiding(closeEvent);
          thisWindow.setOnHidden(closeEvent);
      }
  }
  
  public class TestFXMLLoader extends Application {
      FXMLLoader fxmll;
      FXMLDocumentController fdc;
  
      @Override
      public void start(Stage stage) throws Exception {
          fdc = new FXMLDocumentController();
          fxmll = new FXMLLoader();
          fxmll.setBuilderFactory(new JavaFXBuilderFactory());
          Parent root = fxmll.load(getClass().getResource("FXMLDocument.fxml"));
          fxmll.setController(fdc);
  
          Scene scene = new Scene(root);
  
          stage.setScene(scene);
          stage.show();
      }
  
      /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {
          launch(args);
      }
  
  }
  </code></pre>
  
  <p>And here's the FXML:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;AnchorPane id="AnchorPane" fx:id="apMain" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="testfxmlloader.FXMLDocumentController"&gt;
      &lt;children&gt;
          &lt;Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" /&gt;
          &lt;Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /&gt;
      &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>The <code>windowClosing</code> function should be called before there's even permission to close, so the controls should all be functioning.  The test code above is based off of the NetBeans' default JavaFXML Application, with just the addition of the ReliableOneShotCloseHandler and attendant code.  Is there a flaw in my logic?  Is there another way to do this?</p>
  
  <p><strong>Update:</strong> Some lines of code were included that shouldn't have been. They've been removed.</p>