Skip to content

Exception in Application start method java.lang.reflect.InvocationTargetException

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:

package application;


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;

    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage; // connect primary stage

    // 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();

            // scene on stage
            Scene scene = new Scene(pane);

        } catch (IOException e) {

    public static void main(String[] args) {


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="" xmlns:fx="" fx:controller="application.MainWindowController">
      <Label fx:id="label" alignment="CENTER" layoutX="291.0" layoutY="164.0" text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
            <Font size="20.0" />
      <HBox alignment="CENTER" layoutX="201.0" layoutY="208.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
            <TextField fx:id="field" layoutX="201.0" layoutY="208.0" />
            <Button layoutX="381.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleButton" text="Change Text" />

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();

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.

EDIT: Stack trace here:

Exception in Application start method
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(
    at com.sun.javafx.application.LauncherImpl.launchApplication(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at sun.launcher.LauncherHelper$FXHelper.main(
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(
    at javafx.fxml.FXMLLoader.load(
    at application.Main.mainWindow(
    at application.Main.start(
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(
    at Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(
Exception running application application.Main



For anyone who has this exact same problem in the future, as James_D and the other answer contributors mentioned, removing the “/” at the beginning of the path fixes the problem so use

FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));

instead of

FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));