Exception in Application start method java.lang.reflect.InvocationTargetException
Asked Answered
F

14

8

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:

Main.java

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

MainWindowView.fxml

<?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="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainWindowController">
   <children>
      <Label fx:id="label" alignment="CENTER" layoutX="291.0" layoutY="164.0" text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <font>
            <Font size="20.0" />
         </font>
      </Label>
      <HBox alignment="CENTER" layoutX="201.0" layoutY="208.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <children>
            <TextField fx:id="field" layoutX="201.0" layoutY="208.0" />
            <Button layoutX="381.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleButton" text="Change Text" />
         </children>
      </HBox>
   </children>
</AnchorPane>

MainWindowController.java

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

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
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
Filar answered 9/2, 2016 at 2:0 Comment(2)
That just means it can't find your FXML resource. You're using "/MainWindowView.fxml" as the path, which means it assumes the fxml resource is in the root of the classpath. If you have it in a package, then you need to specify the package name as the path (e.g. "/some/package/MainWindowView.fxml"). If it's in the same package as Main, (i.e. application) then you can just use a relative path: "MainWindowView.fxml"Hollins
Thank you so much, James_D. That solved the problem! Can you post your comment as an answer so I can chose it as the correct answer for other users to see?Filar
F
15

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"));
Filar answered 11/2, 2016 at 3:14 Comment(1)
Actually the funny thing was that I had to do completely oposite operation. I didn't have the "/" and I 've had to put itDarcee
O
5

This problem can also occure even when the path is completely right.

  1. When you create the fxml file in an Updated IDE.

  2. Then use an older JavaFX Scene Builder to design it.

Solution :

  1. Create the fxml file in JavaFX Scane Builder

  2. Design the fxml file in JavaFX Scane Builder then copy this to the IDE or Project.

Omidyar answered 11/3, 2018 at 18:59 Comment(0)
U
2

guess its this ?

@FXML
void handleButton(ActionEvent event) {
Unipod answered 9/2, 2016 at 2:11 Comment(3)
Unfortunately, it still gives the same errors. Thank you for your help, though, checkMyProfile.Filar
That's not the error. This also works with no-args methods (see docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/… last code snippet/paragraph above in the section). However java.lang.IllegalStateException: Location is not set indicates that Main.class.getResource("/MainWindowView.fxml") returns null.Thrombosis
Hello Fabian yes it has been fixed now. It was just the "/" that was causing the issue.Filar
O
1

I also met this case, you please follow me hope to succeed: Tool> Options> Java> JavaFX> Scenne Buider Home> Change Link. Your route to Scenne Buider is unsuitable, it took me hours to find, it is time consuming but it will be a matter of always important when starting JavaFX.

Offer answered 19/12, 2018 at 2:37 Comment(0)
A
1

Perform the following operation

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

Remove / from in front of .fxml file name. I think it should work. It solved the same error for me.

Attest answered 4/5, 2019 at 3:56 Comment(0)
G
0

Location is not set.
This exception states that your FXML file is unreachable by the code. If you have your fxml file inside any package then specify it with the package name.

Grained answered 9/2, 2016 at 6:37 Comment(1)
Hello Prasanna yes it has been fixed now. It was just the "/" that was causing the issue.Filar
M
0

i've had the same problem with the CORRECT PATH i was working in scene builder 8.5 i upgraded it to 11.0 and now its working (i've redesigned all the GUI).

Mulloy answered 16/10, 2019 at 8:15 Comment(0)
C
0

Another cause might be missing VM options:

--module-path <path-to-javafx>/javafx-sdk-11.0.2/lib 
--add-modules=javafx.controls,javafx.fxml
Cleanthes answered 6/3, 2020 at 19:31 Comment(0)
M
0

The same problem happened to me. It's because of maven library. I have different version of jfoenix library in scene builder jfoenix:0.1.8 and in IDE jfoenix:9.0.9.so i downloaded the same version in both and it worked for me.

And also for the nullpointer exception this code worked for me:

FXMLLoader fxmlLoader=new FXMLLoader (Main.class.getResource ("sample.fxml"));
Parent root =fxmlLoader.load ();

The fxml file and Main is in same package.

Montagu answered 8/5, 2020 at 10:31 Comment(0)
V
0

I've been looking through this thread and the answers but the cases are a bit different for me

the code written in the main file is not as mentioned like this:

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

the code in my case is like this:

FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));

and I encountered the same error(approx.)

Verbenia answered 5/11, 2020 at 14:32 Comment(0)
O
0

Solution :

1 : Add Module Path and modules to your VM Options by below command : --module-path <path of your fx library> --add-modules javafx.contros,jafafx.fxml

it should be resolve by above method , if not resolved then in next step

2: create a seperate fxml and add it in your project , it will work .

Overspend answered 8/8, 2021 at 6:38 Comment(0)
L
-1

To reply comment from that's plain wrong .. – kleopatra May seems like that to you, but with same Exception in Application start method java.lang.reflect.InvocationTargetException with .getResource(fxml + ".fxml") as a trigger under JavaFx8 version, that resolve the issue for me and It's the solution, so your comment being plain wrong is personal to issue you and not general. To some it's just the solution they need.

Lazos answered 26/6, 2021 at 0:13 Comment(0)
L
-1

Which is to replace .getResource() with File Class that returns the same URL and the problem solved. There's no need to worry about adding / to the file path or using a relative or absolute file path as long as the file path is valid - check f.exist to be sure. I posted what I've tried and solved after following all possibly thread answers here on this platform with no valid result. This code below solved the issue for me, so if it's plain wrong for you as commented earlier -kleopatra, that's OK. It's not for me.

try { 
    File f = New File("");URL myfile = new File(f.getAbsolutePath()).toURL(); scene.getStylesheets().add(myfile.toExternalForm()); 
    } catch(Exception e) {
}
Lazos answered 26/6, 2021 at 0:22 Comment(0)
L
-2

.getResource("MainWindowView.fxml")); and .getResource(fxml + ".fxml"));

is the problem, known to raise NullPointer and other File not found Exception even when the file path is perfectly valid, get rid of it by using File Class that return same URL value as stated earlier and your code should work fine.

Lazos answered 25/6, 2021 at 2:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.