title: "Not on FX application thread; currentThread = JavaFX Application Thread" and "InvocationTargetException"
body: <p>I'm beginner in java so need help.</p>
  
  <p>I have a javafx app that works with Apache POI (for Excel files).
  When I click menubutton I have an Error in Controller class which should load Excel data in Map collection and then show new Stage window.</p>
  
  <p>But I have a strange error "Not on FX application thread; currentThread = JavaFX Application Thread" when press the button.
  THere is a list of red masseges in output window with such main titles:</p>
  
  <ul>
  <li>Exception in thread "JavaFX Application Thread"
  java.lang.RuntimeException:
  java.lang.reflect.InvocationTargetException 
  
  <ul>
  <li>Caused by: java.lang.reflect.InvocationTargetException </li>
  <li>Caused by: java.lang.IllegalStateException: Not on FX application thread;
  currentThread = JavaFX Application Thread</li>
  </ul></li>
  </ul>
  
  <p>Here is the code for Method in Controller</p>
  
  <pre><code>public void SKULoading(ActionEvent ae) {
              Stage stage = (Stage) menuBar.getScene().getWindow();
              stage.hide();
  
              XSSFWorkbook wb = ExcelModel.OpenFile(stage); //using Filechooser
              ExcelModel book = new ExcelModel(wb);
              Map&lt;Integer, List&lt;String&gt;&gt; data = book.SheetToArray(0);
              book.WBclose();
  
              Stage primarystage;
              // Since that moment error occurs!
              primarystage = new Stage();
              FXMLLoader loader = new FXMLLoader();
              Pane root = null;
              try {
                  root = loader.load(getClass().getResource("/FProject/SKUmanager.fxml").openStream());
              } catch (IOException ex) {
                  Logger.getLogger(MainWindowController.class.getName()).log(Level.SEVERE, null, ex);
              }
              Scene scene = new Scene(root);
              primarystage.setScene(scene);
              primarystage.show();
  } 
  </code></pre>
  
  <p>I saw this topic <a href="https://stackoverflow.com/questions/21083945/how-to-avoid-not-on-fx-application-thread-currentthread-javafx-application-th">How to avoid Not on FX application thread; currentThread = JavaFX Application Thread error?</a> but solution like "Platform.runLater(new Runnable(){});" seems to solve the problem if I wrap in it almost all code in the Method. but I'm not sure it is right desicion because I didn't create any new Thread before.</p>
  
  <ol>
  <li>When I removed row <code>stage.hide();</code> new Stage was shown</li>
  <li><p>Or When I left row <code>stage.hide();</code> and removed rows</p>
  
  <pre><code>    XSSFWorkbook wb = ExcelModel.OpenFile(stage);
      ExcelModel book = new ExcelModel(wb);
      Map&lt;Integer, List&lt;String&gt;&gt; data = book.SheetToArray(0);
      book.WBclose();
  </code></pre>
  
  <p>Stage was shown again.</p></li>
  <li><p>When I replace code</p>
  
  <pre><code>FileChooser fileChooser = new FileChooser();
  fileChooser.setTitle("Open Resource File");
  File file = fileChooser.showOpenDialog(stage);
  </code></pre></li>
  </ol>
  
  <p>in method <code>ExcelModel.OpenFile(stage);</code> by this</p>
  
  <pre><code>File file = new File("C:\\Users\\Maks\\Desktop\\Test.xlsx");
  </code></pre>
  
  <p>Stage was also shown.</p>
  
  <p>So, I think, answer is lying near creating/hiding Stages or using FileChooser <strong>(Looks like using Filechooser somehow influences on the appearence of a new Stage after privious Stage was hidden)</strong></p>
  
  <p><strong>UPD</strong></p>
  
  <pre><code>Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
      at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
      at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
      at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
      ... 43 more
  Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = JavaFX Application Thread
      at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
      at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
      at javafx.stage.Stage.&lt;init&gt;(Stage.java:241)
      at javafx.stage.Stage.&lt;init&gt;(Stage.java:227)
      at FProject.MainWindowController.SKULoading(MainWindowController.java:124)
      ... 53 more
  </code></pre>
  
  <p>Help me to find out what is the root of problem to avoid such errors in future. Thanks!</p>