Issue
I'm currently trying to start coding an app with IntelliJ and JavaFX, I have the following code :
package main.gui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class MainMenu extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("hello-view.fxml"));
URL url = getClass().getResource("hello-view.fxml");
System.out.println("URL = " + url);
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root,1920,1080);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
}
My file structure :
- src/main/gui/MainMenu.java
- src/main/gui/hello-view.fxml
And I get this exception :
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1082)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at main/main.gui.MainMenu.start(MainMenu.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
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:184)
... 1 more
It outputs : URL = null
I've tried a lot of stuff that I've found on this site but without success...
Thank in advance for your help :)
Solution
To add a little more detail : I'm using Maven and I created the project with the JavaFX project creation tool provided with IntelliJ.
I made it work by simply putting my fxml file into a ressources folder (src/main/resources) as suggested by @MatteoNNZ. I tweaked a little bit the code to have the following :
package main.gui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class MainMenu extends Application {
@Override
public void start(Stage stage) throws Exception {
URL url = getClass().getClassLoader().getResource("hello-view.fxml");
System.out.println("URL = " + url);
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root,1920,1080);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
}
Obviously it could be simplified like this :
@Override
public void start(Stage stage) throws Exception {
// URL url = getClass().getClassLoader().getResource("hello-view.fxml"); |
// System.out.println("URL = " + url); | Remove these 3 lines
// Parent root = FXMLLoader.load(url); |
Scene scene = new Scene(FXMLLoader.load(getClass().getClassLoader().getResource("hello-view.fxml")),1920,1080);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
but it is very ugly.
Thank you everyone for your help and very quick answers :) .
Answered By - DIEGO IGLESIAS
Answer Checked By - Pedro (JavaFixing Volunteer)