title: JavaFX - Why does this throw a java.lang.NullPointerException?
body: <p>I have a Stage which loads <code>MainView.fxml</code>. This <code>MainView.fxml</code> includes three other views. In one of these other views I have buttons which I can fully modify without problems. </p>
  
  <p>Here is my <code>MainView.fxml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;BorderPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController"&gt;
     &lt;left&gt;
        &lt;Pane prefHeight="800.0" prefWidth="300.0" BorderPane.alignment="CENTER"&gt;
           &lt;children&gt;
              &lt;fx:include fx:id="ControlsView" source="ControlsView.fxml" /&gt;
              &lt;fx:include fx:id="ParametersView" source="ParametersView.fxml" /&gt;
           &lt;/children&gt;
        &lt;/Pane&gt;
     &lt;/left&gt;
      &lt;center&gt;
          &lt;fx:include fx:id="CashRegisterView" source="CashRegisterView.fxml" /&gt;
      &lt;/center&gt;
  &lt;/BorderPane&gt;
  </code></pre>
  
  <p>For example in the controller for <code>ControlsController</code> I have a method which modiefies buttons which just works fine:</p>
  
  <pre><code>@FXML
  private void btStartActionEventHandler(MouseEvent event){
      if(controlsModel.getBtStartValue() == ControlsModel.buttonStates.Start){
          StartSuperSim();
          controlsModel.setBtStartValue(ControlsModel.buttonStates.Pause);
          btStart.setText(controlsModel.getBtStartValue().toString());
          btStop.setDisable(false);
      }
  }
  </code></pre>
  
  <p>As you can clearly see I modify the properties of <code>btStart</code> and <code>btStop</code>.</p>
  
  <p>But when I try to modify anything coming from the <code>ParametersView.fxml</code> it throws a <code>java.lang.reflect.InvocationTargetException</code>, <code>java.lang.reflect.InvocationTargetException</code> and a <code>java.lang.NullPointerException</code>. As shown <a href="http://pastebin.com/ndHrHCqG" rel="nofollow noreferrer">here</a> in the full stackTrace.</p>
  
  <p>And here is the piece of code in question:</p>
  
  <pre><code>public class CashRegisterController extends BaseController{
  
      @FXML
      private Pane pCashRegister;
  
      @FXML
      public void CreateCashRegisters(){
  
          Pane pane = new Pane();
          pane.setStyle("-fx-background-color: black;");
  
          this.pCashRegister.getChildren().add(0, pane);
      }
  }
  </code></pre>
  
  <p>I don't create a instance of <code>pCashRegister</code> because I don't do that in my <code>ControlsController</code> either. But when I do, it works fine but it doesn't add the <code>pane</code> to my view. </p>
  
  <pre><code>@FXML
  private Button btStart;
  @FXML
  private Button btStop;
  </code></pre>
  
  <p>And at last my <code>CashRegisterView.fxml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.layout.Pane?&gt;
  
  &lt;Pane fx:id="pCashRegister" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.CashRegisterController"&gt;
  </code></pre>
  
  <p></p>
  
  <p>Could somebody please explain how this works and what is happening here? I just can't seem to figure it out. I just think that for whatever reason the <code>CashRegisterView.fxml</code> doesn't link to my <code>CashRegisterController</code> even though I specified <code>fx:controller="controller.CashRegisterController"</code>.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The <code>StartSuperSim()</code> method: </p>
  
  <pre><code>private void StartSuperSim(){
      getCashRegisterController().CreateCashRegisters();
  
      seconds = 0;
  
      setSuperSim(new Timeline(new KeyFrame(Duration.seconds((1 / controlsModel.getAcceleration())), new EventHandler&lt;ActionEvent&gt;() {
          @Override
          public void handle(ActionEvent event) {
              seconds++;
              //System.out.println(seconds);
  
              if((seconds % (int)controlsModel.getParametersModel().getTimeBetweenCustomers()) == 0){
                  //System.out.println("Fired");
              }
          }
      })));
      getSuperSim().setCycleCount(Timeline.INDEFINITE);
      getSuperSim().play();
  }
  </code></pre>
  
  <p>And my <code>MainController.java</code>: </p>
  
  <pre><code>package controller;
  public class MainController extends BaseController{
  
  private ControlsController controlsController;
  private ParametersController parametersController;
  private CashRegisterController cashRegisterController;
  
  public MainController() {
      controlsController = Controller.getInstance().getControlsController();
      parametersController = Controller.getInstance().getParametersController();
      cashRegisterController = Controller.getInstance().getCashRegisterController();
  }
  }
  </code></pre>
  
  <p>And the <code>Controller.java</code> class which I use to pass the instances around:</p>
  
  <pre><code>package controller;
  
  public class Controller {
  private final static Controller instance = new Controller();
  
  public static Controller getInstance() {
      return instance;
  }
  
  private ControlsController controlsController = new ControlsController();
  private ParametersController parametersController = new ParametersController();
  private CashRegisterController cashRegisterController = new CashRegisterController();
  
  public ControlsController getControlsController() {
      return controlsController;
  }
  
  public ParametersController getParametersController() {
      return parametersController;
  }
  
  public CashRegisterController getCashRegisterController() {
      return cashRegisterController;
  }
  }
  </code></pre>
  
  <p>And at last the full stacktrace: </p>
  
  <pre><code>    "D:\Program Files\Java\jdk1.8.0_102\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_102\jre\lib\rt.jar;D:\This PC\Documents\Projects\Java\SuperSim\out\production\SuperSim;D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
  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:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
      at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
      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:380)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
      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: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:1769)
      ... 31 more
  Caused by: java.lang.NullPointerException
      at controller.CashRegisterController.CreateCashRegisters(CashRegisterController.java:26)
      at controller.ControlsController.StartSuperSim(ControlsController.java:95)
      at controller.ControlsController.btStartActionEventHandler(ControlsController.java:69)
      ... 41 more
  </code></pre>