title: Scene Builder 8.0 & Intelli. From one scene to another scene
body: <p>How do I connect two scenes to each other with buttons/menuItems in Scene Builder?</p>
  
  <p>I have done a new test-Project and tried what Hips showed me, but I get some errors. It's not exactly what Hips did so thats why I get errors but not him :-)</p>
  
  <p>Main class.</p>
  
  <pre><code>package sample;
  
  public class Main extends Application {
  
  Stage window;
  Scene mainScene, eventScene;
  BorderPane mainLayout;
  StackPane eventLayout;
  
  @Override
  public void start(Stage primaryStage) throws Exception {
      window = primaryStage;
  
      FXMLLoader loader = new FXMLLoader();
      loader.setLocation(Main.class.getResource("main.fxml"));
      mainLayout = loader.load();
      MainController mainController = loader.getController();
  
      loader = new FXMLLoader();
      loader.setLocation(Main.class.getResource("event.fxml"));
      eventLayout = loader.load();
      EventController eventController = loader.getController();
  
      mainScene = new Scene(mainLayout);
      eventScene = new Scene(eventLayout);
  
  
      window.setScene(mainScene);
      window.setTitle("Test Project");
      window.show();
  
  }
  
  public static void main(String[] args) {
      launch(args);
  }
  
  public void setScene(Scene scene) {
      window.setScene(scene);
  }
  }
  </code></pre>
  
  <p>MainController.</p>
  
  <pre><code>package sample;
  
  
  public class MainController {
  
  private Scene eventScene;
  private Main main;
  
  public void setMain(Main main){
      this.main = main;
  }
  public void seteventScene(Scene eventScene) {
      this.eventScene = eventScene;
  }
  
  @FXML
  public void switchScene() {
      main.setScene(eventScene);
  
  }
  }
  </code></pre>
  
  <p>main.FXML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;BorderPane fx:id="mainLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainController"&gt;
  &lt;top&gt;
    &lt;VBox fx:id="vbox" BorderPane.alignment="CENTER"&gt;
       &lt;children&gt;
          &lt;MenuBar fx:id="menuBar"&gt;
            &lt;menus&gt;
              &lt;Menu fx:id="fileMenu" mnemonicParsing="false" text="File"&gt;
                &lt;items&gt;
                  &lt;MenuItem fx:id="newItem" mnemonicParsing="false" onAction="#switchScene" text="New..." /&gt;
                      &lt;MenuItem fx:id="openItem" mnemonicParsing="false" text="Open..." /&gt;
                      &lt;MenuItem fx:id="saveItem" mnemonicParsing="false" text="Save..." /&gt;
                      &lt;SeparatorMenuItem mnemonicParsing="false" /&gt;
                      &lt;MenuItem fx:id="exitItem" mnemonicParsing="false" text="Exit" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
              &lt;Menu fx:id="editMenu" mnemonicParsing="false" text="Edit"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" text="Delete" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
              &lt;Menu fx:id="helpMenu" mnemonicParsing="false" text="Help"&gt;
                &lt;items&gt;
                  &lt;MenuItem mnemonicParsing="false" text="About" /&gt;
                &lt;/items&gt;
              &lt;/Menu&gt;
            &lt;/menus&gt;
          &lt;/MenuBar&gt;
       &lt;/children&gt;
    &lt;/VBox&gt;
  &lt;/top&gt;
  &lt;/BorderPane&gt;
  </code></pre>
  
  <p>EventController.</p>
  
  <pre><code>package sample;
  
  
  public class EventController {
  
  private Scene mainScene;
  private Main main;
  
  public void setMain(Main main) {
      this.main = main;
  }
  
  public void setmainScene(Scene mainScene) {
      this.mainScene = mainScene;
  
  }
  
  @FXML
  public void goBack() {
      main.setScene(mainScene);
  
  }
  }
  </code></pre>
  
  <p>event.FXML.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  
  &lt;StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.EventController" /&gt;
  </code></pre>
  
  <p>WHEN I click on the 'New...' MenuItem, this is the error I get.</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$355(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$149(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:1771)
  ... 43 more
  Caused by: java.lang.NullPointerException
  at sample.MainController.switchScene(MainController.java:21)
  ... 53 more
  </code></pre>