I just designed an interface using SceneBuilder
and fxml
. When I run the program, it gave me error. May I know what is wrong with the program?
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:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
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:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
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:832)
Caused by: javafx.fxml.LoadException:
/C:/Users/tariq/IdeaProjects/PCIapp/target/classes/com/PCIapp/primary.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2949)
at javafx.fxml/javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2793)
at javafx.fxml/javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2758)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2624)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2517)
at com.PCIapp/com.PCIapp.App.loadFXML(App.java:32)
at com.PCIapp/com.PCIapp.App.start(App.java:20)
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:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
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
Caused by: java.lang.ClassNotFoundException: com.jfoenix.controls.JFXPasswordField
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at javafx.fxml/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:3017)
at javafx.fxml/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:3006)
at javafx.fxml/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2947)
... 16 more
Exception running application com.PCIapp.App
here is the fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" opacity="0.6" prefHeight="800.0" prefWidth="1280.0" style="-fx-background-color: #F5F6FA;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fitHeight="634.0" fitWidth="1204.0" layoutX="38.0" layoutY="83.0" pickOnBounds="true" preserveRatio="true" rotate="-8.0">
<image>
<Image url="@PCI%20logoRYB.png" />
</image></ImageView>
<Pane layoutX="380.0" layoutY="140.0" prefHeight="521.0" prefWidth="520.0" style="-fx-background-color: #DFE4EA; -fx-border-radius: 20; -fx-background-radius: 20;">
<children>
<JFXTextField fx:id="Username" focusColor="#292f5f" labelFloat="true" layoutX="44.0" layoutY="96.0" prefHeight="69.0" prefWidth="432.0" promptText="Username" unFocusColor="#292f5f">
<font>
<Font name="Segoe UI Bold" size="20.0" />
</font>
</JFXTextField>
<JFXPasswordField fx:id="Password" focusColor="#292f5f" labelFloat="true" layoutX="44.0" layoutY="294.0" prefHeight="69.0" prefWidth="432.0" promptText="Password" unFocusColor="#292f5f">
<font>
<Font name="Segoe UI Bold" size="20.0" />
</font>
</JFXPasswordField>
</children>
</Pane>
</children>
</AnchorPane>
and here is the controller"it was pre-written when I created the project and never edited it till now"
package com.PCIapp;
import java.io.IOException;
import javafx.fxml.FXML;
public class PrimaryController {
@FXML
private void switchToSecondary() throws IOException {
App.setRoot("secondary");
}
}
and here is the main file
package com.PCIapp;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
/**
* JavaFX App
*/
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
scene = new Scene(loadFXML("primary"), 1280, 800);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
}
static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch();
}
}
I figured out the answer finally.
it's in the module-info.java file
I just had to put this line of code there
requires com.jfoenix;
but be careful about a thing
when I run the project with a different Jfoenix component, let's say JFXtextfield I encountered "java.lang.reflect.InaccessibleObjectException"
with a large stack trace
someone encountered the same thing right here https://github.com/jfoenixadmin/JFoenix/issues/955
for me since I am using maven on IntelliJ I added those two lines in VM options
--add-opens
java.base/java.lang.reflect=com.jfoenix
--illegal-access=warn
and everything worked well