title: Load new fxml in borderpane center
body: <p>Just started with JavaFx (also a beginner of java generally) and for a few days I've been trying to get this to work but I've been unable to solve it. I'm getting a null pointer exception which I know is covered here:
  <a href="https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it">What is a NullPointerException, and how do I fix it?</a></p>
  
  <p>So I hope this question doesn't get closed as duplicate since I know what npe is and (generally) know how to fix this.</p>
  
  <p>I also know (most of the time) how to dodge them and/or fix them if it occurs but in this case I just don't know how to sort it out.</p>
  
  <p>I've also checked some other questions regarding fxml but still can't figure is out. <br>
  <a href="https://stackoverflow.com/questions/40108443/how-to-change-centerpane-from-leftpane-in-javafx-borderpane">How to change CenterPane from LeftPane in javaFx borderPane?</a> <br>
  <a href="https://stackoverflow.com/questions/32734742/switching-between-panes-in-borderpane-in-javafx">switching between panes in BorderPane in javaFX</a> <br>
  <a href="https://stackoverflow.com/questions/18619394/loading-new-fxml-in-the-same-scene">Loading new fxml in the same scene</a> <br></p>
  
  <p>In my application I want to have a borderpane as a root and in the left side i have three buttons and depending on what button I press a new fxml with a tabpane should load in the center of the borderpane how ever when i press the button i get a npe which i presume is due to me not referencing the right borderpane but i cant see where i'm going wrong.</p>
  
  <p>What I'm trying to do is when i press the button custMenuButton the fxml should load and be set as center in the borderpane.</p>
  
  <p>Main class</p>
  
  <pre><code>import java.io.IOException;
  import javafx.application.Application;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Scene;
  import javafx.scene.layout.BorderPane;
  import javafx.stage.Stage;
  
  public class Main extends Application {
  
      @FXML
      private BorderPane root;
  
      Stage window;
  
      @Override
      public void start(Stage primaryStage) throws IOException {
          window = primaryStage;
          FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
          this.root = loader.load();
  
          MainWindowController mwc = new MainWindowController();
          mwc.setMain(this);
  
          Scene scene = new Scene(root);
          window.setTitle("JavaFX");
          window.setScene(scene);
          window.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  
      public BorderPane getBorderPane() {
          return root;
      }     
  }
  </code></pre>
  
  <p>Main window controller</p>
  
  <pre><code>import java.io.IOException;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.control.Button;
  import javafx.scene.control.TabPane;
  import javafx.scene.layout.BorderPane;
  import javafx.scene.layout.VBox;
  
  public class MainWindowController {
  
      private Main main;
  
      @FXML private BorderPane root;
      @FXML private VBox sideBox;
      @FXML private Button custMenuButton;
      @FXML private Button accMenuButton;
      @FXML private Button transMenuButton; 
  
      public void setMain(Main main) {
          this.main = main;
      }
  
      public void handleCustMenuButton() throws IOException {        
          FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomerView.fxml"));
          TabPane pane = (TabPane) loader.load();
  
          CustomerViewController cvc = loader.getController();
          cvc.setMain(main);
  
          main.getBorderPane().setCenter(pane);
      }
  }
  </code></pre>
  
  <p>Customer view controller</p>
  
  <pre><code>import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Tab;
  import javafx.scene.control.TabPane;
  
  public class CustomerViewController implements Initializable {
  
      Main main;
      @FXML private TabPane custTabPane;
      @FXML private Tab createTab;
      @FXML private Tab listAllTab;
      @FXML private Tab deleteTab;
  
      public void setMain(Main main){
          this.main = main;
      }
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
          // TODO
      }     
  }
  </code></pre>
  
  <p>Mainwindowview</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Menu?&gt;
  &lt;?import javafx.scene.control.MenuBar?&gt;
  &lt;?import javafx.scene.control.MenuItem?&gt;
  &lt;?import javafx.scene.layout.BorderPane?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  
  &lt;BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="firstfx.MainWindowController"&gt;
     &lt;top&gt;
        &lt;MenuBar BorderPane.alignment="CENTER"&gt;
          &lt;menus&gt;
            &lt;Menu mnemonicParsing="false" text="File"&gt;
              &lt;items&gt;
                &lt;MenuItem mnemonicParsing="false" text="Close" /&gt;
              &lt;/items&gt;
            &lt;/Menu&gt;
            &lt;Menu 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;/top&gt;
     &lt;left&gt;
        &lt;VBox fx:id="sideBox" prefHeight="200.0" prefWidth="150.0" spacing="2.0" BorderPane.alignment="CENTER"&gt;
           &lt;children&gt;
              &lt;Button fx:id="custMenuButton" mnemonicParsing="false" onAction="#handleCustMenuButton" prefHeight="50.0" prefWidth="130.0" text="Kundmeny" /&gt;
              &lt;Button fx:id="accMenuButton" mnemonicParsing="false" prefHeight="50.0" prefWidth="130.0" text="Kontomeny" /&gt;
              &lt;Button fx:id="transMenuButton" mnemonicParsing="false" prefHeight="50.0" prefWidth="130.0" text="Transaktioner" /&gt;
           &lt;/children&gt;
           &lt;padding&gt;
              &lt;Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /&gt;
           &lt;/padding&gt;
        &lt;/VBox&gt;
     &lt;/left&gt;
  &lt;/BorderPane&gt;
  </code></pre>
  
  <p>Customer view fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.Tab?&gt;
  &lt;?import javafx.scene.control.TabPane?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;TabPane fx:id="custTabPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="firstfx.CustomerViewController"&gt;
    &lt;tabs&gt;
      &lt;Tab fx:id="createTab" text="Skapa kund"&gt;
        &lt;content&gt;
          &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /&gt;
        &lt;/content&gt;
      &lt;/Tab&gt;
      &lt;Tab fx:id="listAllTab" text="Lista alla kunder"&gt;
        &lt;content&gt;
          &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /&gt;
        &lt;/content&gt;
      &lt;/Tab&gt;
        &lt;Tab fx:id="deleteTab" text="Radera kund"&gt;
          &lt;content&gt;
            &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" /&gt;
          &lt;/content&gt;
        &lt;/Tab&gt;
    &lt;/tabs&gt;
  &lt;/TabPane&gt;
  </code></pre>
  
  <p>Stacktrace</p>
  
  <pre><code>Executing /home/garnbutik/NetBeansProjects/FirstFX/dist/run1223809748/FirstFX.jar using platform /home/garnbutik/jdk1.8.0_151/jre/bin/java
  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.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.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.Node.fireEvent(Node.java:8413)
      at javafx.scene.control.Button.fire(Button.java:185)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      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.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:381)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
      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.gtk.GtkApplication._runLoop(Native Method)
      at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
      at java.lang.Thread.run(Thread.java:748)
  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:498)
      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:498)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
      ... 48 more
  Caused by: java.lang.NullPointerException
      at firstfx.MainWindowController.handleCustMenuButton(MainWindowController.java:31)
      ... 58 more
  BUILD STOPPED (total time: 4 minutes 14 seconds)
  </code></pre>