title: JavaFX Application returning NullPointerException when other Scene is called (just sometimes)
body: <p>My application has a Login Scene and a Main View Scene, what is happening is when I do my login and MainView is called SOMETIMES I get this exception:</p>
  
  <pre><code>java.lang.NullPointerException
      at javafx.scene.Scene.focusInitial(Scene.java:1879)
      at javafx.scene.Scene.access$3600(Scene.java:170)
      at javafx.scene.Scene$ScenePulseListener.focusCleanup(Scene.java:2181)
      at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2221)
      at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:363)
      at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
      at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:329)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
      at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>The curious is it doesn't happening always, <strong>just sometimes.</strong></p>
  
  <p>My class:</p>
  
  <pre><code>public class TargetAppDesktop extends Application {
  
      @Override
      public void start(Stage primaryStage) throws IOException {
  
          Scene scene = new Scene(new AnchorPane());
  
          LoginManager loginManager = new LoginManager(scene);
          loginManager.showLoginScreen();
          primaryStage.setResizable(false);
          primaryStage.setScene(scene);
          primaryStage.show();
  
          primaryStage.setOnCloseRequest(new EventHandler&lt;WindowEvent&gt;() {
              @Override
              public void handle(WindowEvent t) {
                  MainViewController.deleteTempFiles();
                  Platform.exit();
                  System.exit(0);
              }
          });
  
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>MY LOGIN MANAGER CLASS</p>
  
  <pre><code>public class LoginManager {
  
      private Scene scene;
  
      LoginManager(Scene scene) {
          this.scene = scene;
      }
  
      public void logout() {
          showLoginScreen();
      }
  
      void showLoginScreen() {
          try {
              FXMLLoader loader = new FXMLLoader(
                      getClass().getResource("Login.fxml"));
              //         scene.getStylesheets().add(this.getClass().getResource("Login.css").toExternalForm());
  
              scene.setRoot((Parent) loader.load());
              LoginController controller =
                      loader.&lt;LoginController&gt;getController();
              controller.initManager(this);
          } catch (IOException ex) {
              Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      void showMainViewScreen(Login loginTargetApp, Login loginGateway, Gateway gateway, File file, ArrayList&lt;Integer&gt; anoList) {
  
              try {
  
                  FXMLLoader loader = new FXMLLoader(getClass().getResource("MainView.fxml"));
  
                  scene.setRoot((Parent) loader.load());
  
                  MainViewController controller = loader.&lt;MainViewController&gt;getController();
  
                  controller.initSessionID(this, scene, loginTargetApp, loginGateway, gateway, file, anoList);
  
              } catch (Exception ex) {
                  Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex);
              }
  
      }
  
      void autheticated(Login loginTargetApp, Login loginGateway, Gateway gateway, File file, ArrayList&lt;Integer&gt; anoList) {
          showMainViewScreen(loginTargetApp, loginGateway, gateway, file, anoList);
      }
  }
  </code></pre>