title: JavaFX: Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
body: <p>I'm working on JavaFX project and I've an error like this:</p>
  
  <pre><code>Logout
  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.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.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: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.gtk.GtkApplication._runLoop(Native Method)
  at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
  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:1771)
  ... 52 more
  Caused by: java.lang.NullPointerException
  at gn.kahere.car.manager.view.DrawerContentController.logoutAction(DrawerContentController.java:56)
  ... 62 more
  </code></pre>
  
  <p>My MainApp.java</p>
  
  <pre><code>package gn.kahere.car.manager;
  
  import java.io.IOException;
  
  import gn.kahere.car.manager.view.DrawerContentController;
  import gn.kahere.car.manager.view.LoginController;
  import gn.kahere.car.manager.view.RootLayoutController;
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Scene;
  import javafx.scene.layout.AnchorPane;
  import javafx.stage.Stage;
  
  public class MainApp extends Application {
  
      private Stage primaryStage;
      private AnchorPane rootLayout;
  
      @Override
      public void start(Stage primaryStage) {
          this.primaryStage = primaryStage;
          this.primaryStage.setTitle("Car Manager");
  
          // Prevent the window resizing
          this.primaryStage.setResizable(false);
  
          showLogin();
      }
  
      /**
       * Show the login page
       */
      public void showLogin() {
          try {
              // Load login page
              FXMLLoader loader = new FXMLLoader();
              loader.setLocation(MainApp.class.getResource("view/Login.fxml"));
              AnchorPane login = (AnchorPane) loader.load();
  
              // Set the scene containing the login page
              Scene scene = new Scene(login);
              primaryStage.setScene(scene);
  
              // Give the controller access to the main application
              LoginController controller = loader.getController();
              controller.setMainApp(this);
  
              // Show the scene
              primaryStage.show();
          } catch(IOException e) {
              e.printStackTrace();
          }
      }
  
      /**
       * Initializes the root layout
       */
      public void initRootLayout() {
          try {
              // Load root layout
              FXMLLoader loader = new FXMLLoader();
              loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
              rootLayout = (AnchorPane) loader.load();
  
              // Give the controller access to the main application
              RootLayoutController controller = loader.getController();
              controller.setMainApp(this);
  
              // Get the DrawerContentController
              FXMLLoader drawerLoader = new FXMLLoader();
              drawerLoader.setLocation(MainApp.class.getResource("view/DrawerContent.fxml"));
              drawerLoader.load(); // Load the fxml file
              DrawerContentController drawerController = drawerLoader.getController();
              drawerController.setMainApp(this);
  
              // Show the scene containing the root layout
              Scene scene = new Scene(rootLayout);
              primaryStage.setScene(scene);
              primaryStage.show();
          } catch(IOException e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>My RootLayoutController.java</p>
  
  <pre><code>package gn.kahere.car.manager.view;
  
  import java.io.IOException;
  
  import com.jfoenix.controls.JFXDrawer;
  import com.jfoenix.controls.JFXHamburger;
  import com.jfoenix.transitions.hamburger.HamburgerBasicCloseTransition;
  
  import gn.kahere.car.manager.MainApp;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.VBox;
  
  public class RootLayoutController {
  
      // Reference to the main application
      @SuppressWarnings("unused")
      private MainApp mainApp;
  
      @FXML
      private JFXHamburger hamburger;
  
      @FXML
      private JFXDrawer drawer;
  
  
      /**
       * Controller
       */
      public RootLayoutController() {
  
      }
  
      /**
       * Initializes the controller class.
       */
      @FXML
      private void initialize() {
          try {
              VBox box = FXMLLoader.load(getClass().getResource("DrawerContent.fxml"));
              drawer.setSidePane(box);
  
              HamburgerBasicCloseTransition burgerTask = new HamburgerBasicCloseTransition(hamburger);
              burgerTask.setRate(-1);
  
              hamburger.addEventHandler(MouseEvent.MOUSE_PRESSED, (e) -&gt; {
                  burgerTask.setRate(burgerTask.getRate() * -1);
                  burgerTask.play();
  
                  if(drawer.isShown())
                      drawer.close();
                  else
                      drawer.open();
              });
          } catch(IOException e) {
              e.printStackTrace();
          }
      }
  
      /**
       * Is called by the main application to give a reference back to itself
       * 
       * @param mainApp
       */
      public void setMainApp(MainApp mainApp) {
          this.mainApp = mainApp;
      }
  
  }
  </code></pre>
  
  <p>RootLayout.fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import com.jfoenix.controls.JFXDrawer?&gt;
  &lt;?import com.jfoenix.controls.JFXHamburger?&gt;
  &lt;?import javafx.scene.Cursor?&gt;
  &lt;?import javafx.scene.effect.DropShadow?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane prefHeight="700.0" prefWidth="1000.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gn.kahere.car.manager.view.RootLayoutController"&gt;
     &lt;children&gt;
        &lt;JFXHamburger fx:id="hamburger" layoutX="946.0" layoutY="24.0" stylesheets="@style.css" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="20.0"&gt;
           &lt;cursor&gt;
              &lt;Cursor fx:constant="HAND" /&gt;
           &lt;/cursor&gt;
        &lt;/JFXHamburger&gt;
        &lt;JFXDrawer fx:id="drawer" defaultDrawerSize="200.0" prefHeight="700.0" prefWidth="200.0" stylesheets="@../../../../../../bin/gn/kahere/car/manager/view/style.css" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
           &lt;effect&gt;
              &lt;DropShadow /&gt;
           &lt;/effect&gt;
        &lt;/JFXDrawer&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>DrawerContentController.java</p>
  
  <pre><code>package gn.kahere.car.manager.view;
  
  import gn.kahere.car.manager.MainApp;
  import javafx.fxml.FXML;
  
  public class DrawerContentController {
  
      // Reference to the main application
      private MainApp mainApp;
  
  
      /**
       * Controller
       */
      public DrawerContentController() {
  
      }
  
      @FXML
      private void initialize() {
          System.out.println("Drawer");
      }
  
      @FXML
      private void homeAction() {
          System.out.println("Home");
      }
  
      @FXML
      private void addCarAction() {
          System.out.println("Add Car");
      }
  
      @FXML
      private void listCarsAction() {
          System.out.println("List Cars");
      }
  
      @FXML
      private void searchAction() {
          System.out.println("Search");
      }
  
      @FXML
      private void agentsAction() {
          System.out.println("Agents");
      }
  
      @FXML
      private void profileAction() {
          System.out.println("Profile");
      }
  
      @FXML
      private void logoutAction() {
          System.out.println("Logout");
          mainApp.showLogin();
      }
  
      /**
       * Is called by the main application to give a reference back to itself
       * 
       * @param mainApp
       */
      public void setMainApp(MainApp mainApp) {
          this.mainApp = mainApp;
      }
  
  }
  </code></pre>
  
  <p>DrawerContent.fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import com.jfoenix.controls.JFXButton?&gt;
  &lt;?import java.lang.String?&gt;
  &lt;?import javafx.scene.control.Separator?&gt;
  &lt;?import javafx.scene.image.Image?&gt;
  &lt;?import javafx.scene.image.ImageView?&gt;
  &lt;?import javafx.scene.layout.Pane?&gt;
  &lt;?import javafx.scene.layout.VBox?&gt;
  &lt;?import javafx.scene.text.Font?&gt;
  &lt;?import javafx.scene.text.Text?&gt;
  &lt;?import javafx.scene.text.TextFlow?&gt;
  
  &lt;VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="200.0" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gn.kahere.car.manager.view.DrawerContentController"&gt;
     &lt;children&gt;
        &lt;Pane prefHeight="150.0" prefWidth="200.0" styleClass="side-menu-header" stylesheets="@style.css"&gt;
           &lt;children&gt;
              &lt;Text fill="WHITE" layoutX="14.0" layoutY="131.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MAMADOU ALIOU DIALLO"&gt;
                 &lt;font&gt;
                    &lt;Font size="12.0" /&gt;
                 &lt;/font&gt;
              &lt;/Text&gt;
              &lt;ImageView fitHeight="62.0" fitWidth="66.0" layoutX="14.0" layoutY="44.0" pickOnBounds="true" preserveRatio="true"&gt;
                 &lt;image&gt;
                    &lt;Image url="@../../../../../../img/user/avatar.png" /&gt;
                 &lt;/image&gt;
              &lt;/ImageView&gt;
           &lt;/children&gt;
        &lt;/Pane&gt;
        &lt;TextFlow prefHeight="18.0" prefWidth="200.0" style="-fx-background-color: #f0f8ff;" styleClass="navigation" stylesheets="@style.css"&gt;
           &lt;children&gt;
              &lt;Text strokeType="OUTSIDE" strokeWidth="0.0" text="PRINCIPAL NAVIGATION" wrappingWidth="176.49267578125" /&gt;
           &lt;/children&gt;
        &lt;/TextFlow&gt;
        &lt;JFXButton buttonType="RAISED" onAction="#homeAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="HOME" textFill="WHITE"&gt;
           &lt;font&gt;
              &lt;Font size="16.0" /&gt;
           &lt;/font&gt;
           &lt;styleClass&gt;
              &lt;String fx:value="menu-btn" /&gt;
              &lt;String fx:value="btn" /&gt;
           &lt;/styleClass&gt;
        &lt;/JFXButton&gt;
        &lt;Separator prefWidth="200.0" /&gt;
        &lt;JFXButton buttonType="RAISED" onAction="#addCarAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="ADD CAR" textFill="WHITE"&gt;
           &lt;font&gt;
              &lt;Font size="16.0" /&gt;
           &lt;/font&gt;
           &lt;styleClass&gt;
              &lt;String fx:value="menu-btn" /&gt;
              &lt;String fx:value="btn" /&gt;
           &lt;/styleClass&gt;
        &lt;/JFXButton&gt;
        &lt;Separator prefWidth="200.0" /&gt;
        &lt;JFXButton buttonType="RAISED" layoutX="10.0" layoutY="196.0" onAction="#listCarsAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="LIST CARS" textFill="WHITE"&gt;
           &lt;font&gt;
              &lt;Font size="16.0" /&gt;
           &lt;/font&gt;
           &lt;styleClass&gt;
              &lt;String fx:value="menu-btn" /&gt;
              &lt;String fx:value="btn" /&gt;
           &lt;/styleClass&gt;
        &lt;/JFXButton&gt;
        &lt;Separator prefWidth="200.0" /&gt;
        &lt;JFXButton buttonType="RAISED" layoutX="10.0" layoutY="196.0" onAction="#searchAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="SEARCH" textFill="WHITE"&gt;
           &lt;font&gt;
              &lt;Font size="16.0" /&gt;
           &lt;/font&gt;
           &lt;styleClass&gt;
              &lt;String fx:value="menu-btn" /&gt;
              &lt;String fx:value="btn" /&gt;
           &lt;/styleClass&gt;
        &lt;/JFXButton&gt;
        &lt;TextFlow layoutX="10.0" layoutY="160.0" prefHeight="18.0" prefWidth="200.0" style="-fx-background-color: #f0f8ff;" styleClass="navigation" stylesheets="@style.css"&gt;
           &lt;children&gt;
              &lt;Text strokeType="OUTSIDE" strokeWidth="0.0" text="MANAGER" wrappingWidth="176.49267578125" /&gt;
           &lt;/children&gt;
        &lt;/TextFlow&gt;
        &lt;JFXButton buttonType="RAISED" layoutX="10.0" layoutY="196.0" onAction="#agentsAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="AGENTS" textFill="WHITE"&gt;
           &lt;font&gt;
              &lt;Font size="16.0" /&gt;
           &lt;/font&gt;
           &lt;styleClass&gt;
              &lt;String fx:value="menu-btn" /&gt;
              &lt;String fx:value="btn" /&gt;
           &lt;/styleClass&gt;
        &lt;/JFXButton&gt;
        &lt;Separator prefWidth="200.0" /&gt;
        &lt;JFXButton buttonType="RAISED" layoutX="10.0" layoutY="408.0" onAction="#profileAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="PROFILE" textFill="WHITE"&gt;
           &lt;font&gt;
              &lt;Font size="16.0" /&gt;
           &lt;/font&gt;
           &lt;styleClass&gt;
              &lt;String fx:value="menu-btn" /&gt;
              &lt;String fx:value="btn" /&gt;
           &lt;/styleClass&gt;
        &lt;/JFXButton&gt;
        &lt;Separator prefWidth="200.0" /&gt;
        &lt;JFXButton buttonType="RAISED" layoutX="10.0" layoutY="441.0" onAction="#logoutAction" prefHeight="50.0" prefWidth="199.0" style="-fx-background-color: #ff033e;" stylesheets="@style.css" text="LOGOUT" textFill="WHITE"&gt;
           &lt;font&gt;
              &lt;Font size="16.0" /&gt;
           &lt;/font&gt;
           &lt;styleClass&gt;
              &lt;String fx:value="menu-btn" /&gt;
              &lt;String fx:value="btn" /&gt;
           &lt;/styleClass&gt;
        &lt;/JFXButton&gt;
     &lt;/children&gt;
  &lt;/VBox&gt;
  </code></pre>
  
  <p>The error occurs when I click on the logout button in DefaultContentController, the "Logout" message is displayed, but I don't understand that error. Help please. Thks.</p>