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.