InvocationTargetException in JavaFX

Sheeraz Gul Oct 12, 2023
InvocationTargetException in JavaFX

This tutorial demonstrates how to solve the InvocationTargetException in JavaFX.

Fix InvocationTargetException in JavaFX

The exception InvocationTargetException occurs when we are using JavaFX. The reasons this exception occurs are:

  1. When we write a forward slash at the start of the path name of the FXML file or the path given is not correct or complete.
  2. When we try to use dynamic root but haven’t set the root on the FXML Loader.
  3. When we miss a JavaFX Module.

Let’s try an example that will throw the InvocationTargetException in JavaFX.

package delftstack;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Example extends Application {
  public void start(Stage DemoStage) throws Exception {
    Parent DemoRoot = FXMLLoader.load(getClass().getResource("delftstack.fxml"));
    Scene DemoScene = new Scene(DemoRoot, 200, 150);

The delftstack.fxml file is:

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

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="" xmlns:fx="">
      <ScrollPane layoutX="178.0" layoutY="68.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <TableView prefHeight="350.0" prefWidth="590.0">
                  <TableColumn prefWidth="130.0" text="First Name" />
                <TableColumn prefWidth="130.0" text="Last Name" />
                <TableColumn prefWidth="330.0" text="ID" />

The code above will throw the InvocationTargetException because it couldn’t find the delftstack.fxml file. See output:

Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.base/java.lang.reflect.Method.invoke(
	at java.base/sun.launcher.LauncherHelper$FXHelper.main(
Caused by: java.lang.RuntimeException: Exception in Application start method
	at java.base/
Caused by: java.lang.NullPointerException: Location is required.
	at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(
	at delftstack.Example.start(
	at java.base/
	at Method)
	... 1 more

Now, if we try to solve it by giving the absolute path for the file delftstack.fxml, it may solve the problem, but for some systems, it can still cause the same problem. The best solution for this error is given below:

  1. Copy the delftstack.fxml file in the same directory of the class. For example, if the class file is in the delftstack package, the FXML file should be copied to the project/src/delftstack folder.
  2. Refresh the project in the IDE. For example, in Eclipse, right-click on the project name and select Refresh.
  3. Run the same program, and the error will be solved.

The output after following the above solution is:


Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java JavaFX

Related Article - Java Error