title: JavaFx 2 - Setting Pane with fxml dynamically
body: <p>I am trying to set center pane of border pane layout dynamically by loading from fxml file. Right now, I am encountering null pointer exception when I invoke setCenterPane() method. Below is the controller code that load and set center pane. The type of pane that I am trying to load is AnchorPane, and I have tried to cast it to AnchorPane too, but it still behaves the same. I am not sure if I am missing something. Could anyone provide any idea on how to solve this issue?  </p>
  
  <p>The null pointer exception is throwing form <strong><em>borderPane.setCenter(cmdPane)</em></strong> line. (I remove some lines form my code for readability).</p>
  
  <pre><code>public class AnalyzerController implements Initializable {
  
  @FXML
  private BorderPane borderPane;
  
  @FXML
  private void cmdMenuAction(ActionEvent event) throws IOException {
          FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CommandQuery.fxml"));
          Pane cmdPane = (Pane) fxmlLoader.load();
  
          try {
              borderPane.setCenter(cmdPane);
          } catch (Exception e) {
              e.printStackTrace();
          }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Below is the exception trace.</p>
  
  <pre><code>java.lang.NullPointerException
      at analyzer.AnalyzerController.cmdMenuAction(AnalyzerController.java:65)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:55)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:269)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
      at javafx.event.Event.fireEvent(Event.java:171)
      at javafx.scene.Node.fireEvent(Node.java:6863)
      at javafx.scene.control.Hyperlink.fire(Hyperlink.java:145)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
      at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
      at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
      at javafx.event.Event.fireEvent(Event.java:171)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
      at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
      at com.sun.glass.ui.View.notifyMouse(View.java:922)
  </code></pre>
  
  <p>This is Analyzer.fxml referenced by AnalyzerController</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.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.paint.*?&gt;
  
  &lt;BorderPane id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="analyzer.AnalyzerController"&gt;
    &lt;center&gt;
      &lt;Pane prefHeight="200.0" prefWidth="200.0"&gt;
        &lt;children&gt;
          &lt;Label fx:id="noDbLabel" layoutX="187.0" layoutY="99.0" text="No database selected." /&gt;
        &lt;/children&gt;
      &lt;/Pane&gt;
    &lt;/center&gt;
    &lt;left&gt;
      &lt;Hyperlink fx:id="cmdMenu" onAction="#cmdMenuAction" text="Command" /&gt;
    &lt;/left&gt;
    &lt;top&gt;
      &lt;MenuBar&gt;
        &lt;menus&gt;
          &lt;Menu mnemonicParsing="false" text="File"&gt;
            &lt;items&gt;
              &lt;MenuItem mnemonicParsing="false" onAction="#openDbAction" text="Open Database" fx:id="OpenDb" /&gt;
            &lt;/items&gt;
          &lt;/Menu&gt;
        &lt;/menus&gt;
      &lt;/MenuBar&gt;
    &lt;/top&gt;
  &lt;/BorderPane&gt;
  </code></pre>
  
  <p>Here is CommandQuery.fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.net.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.collections.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="analyzer.CommandQueryController"&gt;
    &lt;children&gt;
      &lt;SplitPane dividerPositions="0.29396984924623115" focusTraversable="true" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
        &lt;items&gt;
          &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"&gt;
            &lt;children&gt;
              &lt;Label layoutX="36.0" layoutY="23.0" text="Find" /&gt;
              &lt;ComboBox fx:id="cmdTypeBox" layoutX="78.0" layoutY="23.0"&gt;
                &lt;items&gt;
                  &lt;FXCollections fx:factory="observableArrayList"&gt;
                    &lt;String fx:value="Item 1" /&gt;
                    &lt;String fx:value="Item 2" /&gt;
                    &lt;String fx:value="Item 3" /&gt;
                  &lt;/FXCollections&gt;
                &lt;/items&gt;
              &lt;/ComboBox&gt;
              &lt;Label layoutX="177.0" layoutY="23.0" text="From" /&gt;
            &lt;/children&gt;
          &lt;/AnchorPane&gt;
          &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" /&gt;
        &lt;/items&gt;
      &lt;/SplitPane&gt;
    &lt;/children&gt;
    &lt;stylesheets&gt;
      &lt;URL value="@commandquery.css" /&gt;
    &lt;/stylesheets&gt;
  &lt;/AnchorPane&gt;
  </code></pre>