1

I've been looking for a long time for the answer, however none of the other questions had an answer to my problem.

I'm creating a simple App (a simple Form) in JavaFX using SceneBuilder. Sadly I cannot run the code as it keeps throwing LoadException.

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#login', either the event handler is not in the Namespace or there is an error in the script.
/D:/JavaApps/FormApp/out/production/FormApp/master/loginForm/loginForm.fxml:30

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2703)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:620)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:780)
    at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2517)
    at JavaFxApplication/master.Main.start(Main.java:26)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application master.Main

Here's the FXML content - as you can see I have added the controller.

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<GridPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="master.loginForm.LoginForm">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <Pane prefHeight="400.0" prefWidth="200.0">
         <children>
            <Label layoutX="26.0" layoutY="132.0" prefHeight="17.0" prefWidth="149.0" text="Surname" textAlignment="CENTER" />
            <Label layoutX="26.0" layoutY="6.0" prefHeight="17.0" prefWidth="149.0" text="Nick" textAlignment="CENTER" />
            <TextField fx:id="nickTF" layoutX="26.0" layoutY="25.0" promptText="Your awesome nick" />
            <Label layoutX="30.0" layoutY="71.0" prefHeight="17.0" prefWidth="141.0" text="Name" textAlignment="CENTER" />
            <TextField fx:id="nameTF" layoutX="26.0" layoutY="88.0" promptText="Name" />
            <TextField fx:id="surnameTF" layoutX="26.0" layoutY="149.0" promptText="Surname" />
            <Pane layoutY="200.0" prefHeight="59.0" prefWidth="200.0">
               <children>
                  <RadioButton fx:id="maleRB" layoutX="29.0" mnemonicParsing="false" text="Male" toggleGroup="$sexTG"></RadioButton>
                  <RadioButton fx:id="femaleRB" layoutX="29.0" layoutY="29.0" mnemonicParsing="false" text="Female" toggleGroup="$sexTG" />
               </children>
            </Pane>
            <TextField fx:id="emailTF" layoutX="26.0" layoutY="280.0" promptText="email@gra.pl" />
            <Label layoutX="26.0" layoutY="259.0" prefHeight="17.0" prefWidth="149.0" text="Email" textAlignment="CENTER" />
            <Button fx:id="loginBT" defaultButton="true" layoutX="74.0" layoutY="336.0" onAction="#login" text="Login"> </Button>
         </children>
      </Pane>
      <Pane fx:id="exceptionNotifyPane" layoutX="200.0" layoutY="14.0" prefHeight="164.0" prefWidth="149.0" />
   </children>
</GridPane>

Here's the content of the controler - mind you, I've tried both methods with and without ActionEvent parameter in case it might have been the issue, which it clearly is not.

package master.loginForm;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Pane;

public class LoginForm {

    @FXML
    private TextField nameTF;

    @FXML
    private TextField surnameTF;

    @FXML
    private TextField emailTF;

    @FXML
    private TextField nickTF;

    @FXML
    private Pane exceptionNotifyPane;

    @FXML
    private ToggleGroup sexTG;

    @FXML
    private Button loginBT;

    @FXML
    public void initialize(){

    }

    @FXML
    public void login(ActionEvent event){

    }
}

Lastly here's the Main content.

package master;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.LoadException;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import master.loginForm.LoginForm;

import java.net.URL;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        try {
            primaryStage.setTitle("Login Form");

            URL resource = LoginForm.class.getResource("loginForm.fxml");

            FXMLLoader loader = new FXMLLoader(resource);
            loader.setControllerFactory(param -> this);
            loader.setClassLoader(getClass().getClassLoader());

            Parent root = loader.load();

            Scene scene = new Scene(root);

            primaryStage.setScene(scene);
            primaryStage.show();

        }catch (LoadException loadException){
            System.out.println("LoadException occurred " + loadException.getMessage());
            return;
        }
        catch(Exception e){
            System.out.println("Exception occurred: " + e.getClass().getSimpleName() + "\n" + e.getMessage());
            return;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

I've had a problem before with this code with the same exception, which was "solved" by removing the fx:controller line from the FXML. Obviously this had become a problem once event handling was necessary in the code. I've tried to implement evenHandler inside the controller instead, but this did not work either resulting in the same Exception. After many hours of research and a failure in finding the solution I'd gladly accept any help, thank you.

  • It works when you remove the controller factory statement `loader.setControllerFactory(param -> this);` – anko May 11 '21 at 11:13
  • @anko Sadly it does not, the Exception still occurs – RolePoloGame May 11 '21 at 11:16
  • [mcve] please .. including the complete stacktrace (don't swallow it nor print its message only) – kleopatra May 11 '21 at 11:34
  • @kleopatra sorry about that, I have put the whole stacktrace (if I'm not mistaken) – RolePoloGame May 11 '21 at 11:42
  • not quite minimal but short enough to look over it :) Curious (and have no IDE handy at the moment to try it myself, sry): why are you setting the controllerFactory and classloader? What happens if you don't? – kleopatra May 11 '21 at 12:07
  • @kleopatra at the begining that fixed the problem with the LoadException, however once event handlers were added it stopped working. – RolePoloGame May 11 '21 at 13:43
  • worksforme when commenting the setControllerFactory/setClassLoader lines - remove them and then start from what you get then. When still stuck, change the example here to demonstrate what's wrong (while you are at it: make it _really_ minimal, make sure the code is the _exact_ code that throws the exception if any, stick to java naming conventions - package names should be all lowercase) – kleopatra May 12 '21 at 10:00
  • bottom line: don't use api you don't understand just because it _appears_ to solve some problem! If you _really_ need to set a custom controllerFactory, do some research on what it is supposed to do and then use it in the way it is supposed to be used (as the name hints to: it should return an instance of the _controller_ .. not the application) BTW: had to do some research myself, as I don't use fxml too often :) – kleopatra May 12 '21 at 10:23
  • @kleopatra thank you for all that information - I must say that because of the time pressure I began to panic a bit as I had to have it ready as soon as possible and made a lot of rookie mistakes indeed despite the experience with programming in general. Will try to be more careful next time ;) – RolePoloGame May 12 '21 at 21:47

1 Answers1

0

For those seeking answer to the same exact problem I might have a solution for you. Creating the same exact project "from scratch" resulted in the same problem.

The solution for me was to update the project's SDK (redownloaded it using my IDE).

EDIT 1:
As kleopatra said in the comments, it is worth mentioning that I was INCORRECTLY using the setControllerFactory. I removed that part from my code completely and have set the controller only in FXML file.

EDIT 2:
As it turned out the previous solution was not a solution at all.

First of all make sure you are creating your new window properly

In my case, I've created such function:

private void createNewWindow(String resource, String title) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(resource));
            Parent newRoot = fxmlLoader.load();
            Stage stage = new Stage();
            stage.setTitle(title);
            stage.setScene(new Scene(newRoot));
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Which can simply be called when needed:

createNewWindow("/resources/fxmFile.fxml", "Window Title");

Make sure you're referencing your .fxml file properly

Second of all make sure you added your controller in your FXML file.

<AnchorPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="main.packagename.ControllerName"
>
...
</AnchorPane>

Lastly make sure that your module-info.java file is configured properly:

    //all necessary java requires including those below
    requires javafx.base;
    requires javafx.graphics;
    requires javafx.fxml;
    requires javafx.controls;

    //assuming your main package is called 'main'
    exports main;
    opens main;

    exports main.subPackageName;
    opens main.subPackageName;
    ...
    exports main.otherSubPackageName;
    opens main.otherSubPackageName;

Additionally make sure to include your java.fx lib folder in your IDE.

This has solved all of my issues, hope that it helps someone one day ;).

  • @kleopatra it does not, however the source of the problem was not in the incorrect usage; I did remove it from my code and I'll add that hint to the answer for others to know that too though. – RolePoloGame May 14 '21 at 11:08