1

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

fabian
  • 80,457
  • 12
  • 86
  • 114
Chukkee
  • 3
  • 5
  • `getClass().getResource(...)` will search relative to the current class. So this is expecting `Main.fxml` to be in the `application.application` package. – James_D Jan 08 '18 at 22:36
  • Does this answer your question? [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) – kleopatra May 04 '22 at 22:59

3 Answers3

0

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.

jsaf
  • 59
  • 4
0

this will work:

"/application/Main.fxml"

you just need to add a /

Duo.Liu
  • 63
  • 6
-3

Solved the problem by dragging and dropping Main.fxml in src folder.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Chukkee
  • 3
  • 5