'java.lang.NullPointerException: Location is required still not working
I have also run into some problems trying to get my JavaFX program to run. Like some of the others, I keep getting an error of java.lang.NullPointerException: Location is required.
The fxml file is in the Application package. I've tried all the remedies I've found in here, but maybe I'm missing something? Here is my code
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("application/Main.fxml"));
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
And here is the Error
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 application.Main.start(Main.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Any help would be much appreciated. Thanks in advance
Solution 1:[1]
I lost an entire day battling with this issue in Netbeans. This is how I solved it : Firstly, I put the fxml file in the same package with main (though it can work even in separate packages). Secondly, I used getClassLoader() specifying the path of the file. Here's the code :` public class Main {
public static void main(String[] args) {
System.out.println("Test = "+Main.class.getClassLoader().getResource("FXMLDocument.fxml"));`
After Running the project, here's the output : ....NetBeansProjects/JavaFXApplicationTest/dist/JavaFXApplicationTest.jar!/FXMLDocument.fxml Note that Running the file alone was generating an error before.
Solution 2:[2]
this will work:
"/application/Main.fxml"
you just need to add a /
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jsaf |
Solution 2 | Duo.Liu |