Reputation: 3
I'm creating a simple JavaFX app and it's throwing InvocationTargetException
.
The exception that's thrown pertains to error(s) in the path specification when I try to load an FXML file; therefore, to make sure this was really the reason behind the exception, I tried creating a simple JavaFX application that displays a button in the scene. The same exception was thrown regardless.
I have found some solved questions about this and i tried all the solutions such as removing
the \ from the fxml file path and creating an instance of FXMLLoader
instead of relying on the static load(...)
method. None of these worked for me.
Main Class:
package UI;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainFX extends Application {
private Stage stage;
private BorderPane borderPane;
@Override
public void start(Stage stage) throws IOException {
this.stage = stage;
this.stage.setTitle("App");
showMainView();
}
private void showMainView() throws IOException
{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainFX.class.getResource("login.fxml"));
borderPane = loader.load();
Scene scene = new Scene(borderPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
login.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXSpinner?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.effect.Light.Distant?>
<?import javafx.scene.effect.Lighting?>
<?import javafx.scene.effect.Shadow?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" style="-fx-background-color: #222629;" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
<center>
<GridPane BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="208.0" minWidth="10.0" prefWidth="166.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="414.0" minWidth="10.0" prefWidth="266.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="168.0" minWidth="10.0" prefWidth="168.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="5.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints fillHeight="false" maxHeight="-Infinity" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<JFXTextField focusColor="#999ea1" promptText="Email" style="-fx-text-inner-color: #999ea1;" unFocusColor="#474b4f" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<JFXPasswordField focusColor="#999ea1" labelFloat="true" promptText="Password" style="-fx-text-inner-color: #999ea1;" unFocusColor="#474b4f" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Text fill="WHITE" lineSpacing="50.0" pickOnBounds="true" smooth="false" strokeWidth="0.0" text="Login" textAlignment="CENTER" wrappingWidth="265.443359375" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font name="Arial Bold" size="34.0" />
</font>
</Text>
<GridPane GridPane.columnIndex="1" GridPane.rowIndex="7">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<JFXButton alignment="TOP_CENTER" prefHeight="25.0" prefWidth="135.0" ripplerFill="#979797" text="Cancel" textFill="WHITE" />
<JFXButton alignment="TOP_CENTER" prefHeight="25.0" prefWidth="137.0" ripplerFill="#86c232" style="-fx-background-color: #61892F;" text="Login" textFill="WHITE" GridPane.columnIndex="1" />
</children>
</GridPane>
<JFXSpinner prefHeight="22.0" prefWidth="198.0" GridPane.columnIndex="1" GridPane.rowIndex="6">
<effect>
<Lighting>
<bumpInput>
<Shadow />
</bumpInput>
<light>
<Light.Distant />
</light>
</Lighting>
</effect>
</JFXSpinner>
</children>
</GridPane>
</center>
</BorderPane>
stack-trace:
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:498)
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:498)
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/User/Documents/NetBeansProjects/ JavaFXApplication2/dist/run966830135/JavaFXApplication2.jar!/login.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.ClassNotFoundException: com.jfoenix.controls.JFXButton
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 20 more
Exception running application Main
Java Result: 1
Upvotes: 0
Views: 163
Reputation: 8363
You have imported Jfoenix API into your scenebuilder but you did not include it into your project's build path.
The exact way to add the JAR file to your project depends on your IDE. You can do a quick google and find it out on your own.
Upvotes: 1