title: Exception in Application start method java.lang.reflect.InvocationTargetException
body: <p>I am just starting out with JavaFX, and I am trying to build a simple application with a label, text field and button which, when clicked, sets the label's value to that of the text field's. Everything was going well until I connected the controller to the Main file. Here's my code:</p>
  
  <p>Main.java</p>
  
  <pre><code>package application;
  
  import java.io.IOException;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.stage.Stage;
  import javafx.scene.Scene;
  import javafx.scene.layout.AnchorPane;
  
  
  public class Main extends Application {
  
      private Stage primaryStage;
  
      @Override
      public void start(Stage primaryStage) {
          this.primaryStage = primaryStage; // connect primary stage
          mainWindow();
      }
  
      // main window
      public void mainWindow() {
          try {
              // view
              FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
              AnchorPane pane = loader.load();
  
              // controller
              MainWindowController mainWindowController = loader.getController();
              mainWindowController.setMain(this);
  
              // scene on stage
              Scene scene = new Scene(pane);
              primaryStage.setScene(scene);
              primaryStage.show();
  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>MainWindowView.fxml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.text.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainWindowController"&gt;
     &lt;children&gt;
        &lt;Label fx:id="label" alignment="CENTER" layoutX="291.0" layoutY="164.0" text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"&gt;
           &lt;font&gt;
              &lt;Font size="20.0" /&gt;
           &lt;/font&gt;
        &lt;/Label&gt;
        &lt;HBox alignment="CENTER" layoutX="201.0" layoutY="208.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"&gt;
           &lt;children&gt;
              &lt;TextField fx:id="field" layoutX="201.0" layoutY="208.0" /&gt;
              &lt;Button layoutX="381.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleButton" text="Change Text" /&gt;
           &lt;/children&gt;
        &lt;/HBox&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>MainWindowController.java</p>
  
  <pre><code>package application;
  
  import javafx.fxml.FXML;
  import javafx.scene.control.Label;
  import javafx.scene.control.TextField;
  
  public class MainWindowController {
  
      // views
      @FXML private Label label;
      @FXML private TextField field;
  
      private Main main;
  
      // connect main class to controller
      public void setMain(Main main) {
          this.main = main; 
      }
  
      // assign text field text to label on button click
      public void handleButton() {
          String text = field.getText();
          label.setText(text);
          field.clear();
      }
  }
  </code></pre>
  
  <p>I have tried multiple answers found on StackOverflow, but all that I have found were from 2 years ago and did not make any positive effect on my code. </p>
  
  <p>EDIT: Stack trace here:</p>
  
  <pre><code>Exception in Application start method
  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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
      at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
      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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  Caused by: java.lang.RuntimeException: Exception in Application start method
      at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
      at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.IllegalStateException: Location is not set.
      at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
      at application.Main.mainWindow(Main.java:27)
      at application.Main.start(Main.java:19)
      at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
      at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
      at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
      at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  Exception running application application.Main
  </code></pre>