The question is published on by Tutorial Guruji team.
I have created a simple javaFX program and i added spring boot to javaFX by making the project a maven project. Before adding spring boot i got no error and the application worked fine. I have provided the complete code below
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> </parent> <groupId>groupId</groupId> <artifactId>RMI-DesktopClient</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
AlarmsystemApplication.java
@Configuration @SpringBootApplication public class AlarmSystemApplication extends Application { private ConfigurableApplicationContext applicationContext; @Override public void init() throws Exception { this.applicationContext = SpringApplication.run(AlarmSystemApplication.class); } @Override public void stop() throws Exception { applicationContext.close(); } public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { Login.loadView(stage); } }
Login.java
public class Login { public static void loadView(Stage stage) { try { Parent view = FXMLLoader.load(Login.class.getResource("../../../../../resources/com.ui.views/Login.fxml")); stage.setScene(new Scene(view)); stage.show(); } catch (IOException e) { e.printStackTrace(); } } }
Login.fxml
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.alarmsystem.ui.views.Login"> <children> <Label layoutX="137.0" layoutY="157.0" text="Hello JavaFX"> <font> <Font size="59.0" /> </font> </Label> </children> </AnchorPane>
The error that i get
Exception in Application start method 2020-04-23 22:33:12.134 INFO 7228 --- [lication Thread] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2020-04-23 22:33:12.136 INFO 7228 --- [lication Thread] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2020-04-23 22:33:12.139 INFO 7228 --- [lication Thread] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 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: java.lang.NullPointerException: Location is required. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 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 com.alarmsystem.ui.views.Login.loadView(Login.java:16) at com.alarmsystem.ui.AlarmSystemApplication.start(AlarmSystemApplication.java:34) 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 Exception running application com.alarmsystem.ui.AlarmSystemApplication Process finished with exit code 1
Answer
I’m not sure this is Spring related; the resource name you use for your FXML is not a valid resource name (and the stack trace indicates that not being able to find the FXML file is the issue).
Specifically, resource names cannot have .
in them, so ..
and com.ui.views
are both invalid.
So there are two problems here: one is that the resources name doesn’t work with “parent directories” specified in it, and the other is that you have created a folder (not a package) under resources
with illegal .
characters in it. Also note that resources
is a source folder, and is not available at runtime.
So, first, create a package under resources called com.ui.views
and place the FMXL in there. I don’t use IntelliJ, so I’m not sure if there is an option to do this, but if not you can create a folder com
, a subfolder of it called ui
, and a subfolder of that called views
, and place the FXML file in views
.
Then the correct resource name for the FXML, assuming you are using Maven defaults for your build, is
Parent view = FXMLLoader.load(Login.class.getResource("/com/ui/views/Login.fxml"));
If you restructure slightly so that the FXML file and Login
class are in the same package (i.e. you make a com.alarmsystem.ui.views
package under resources
), then you can just do
Parent view = FXMLLoader.load(Login.class.getResource("Login.fxml"));
If you need to troubleshoot further, you can see where the resources were deployed (so where they are found at runtime), by looking in the target/classes
folder.