Matthias Brück
Matthias Brück

Reputation: 204

JavaFX Error with Netbeans Standard FXML application - Nullpointer for .fxml file

When I create a new Netbeans Projekt (JavaFX FXML Application) with application class I receive a template as usual (one .fxml, one controller class and the main application). When I try to run this I get an exception:

Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2825)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
    at volltextsuche.Volltextsuche.start(Volltextsuche.java:25)
    [...]

This is my start() method:

@Override
public void start(Stage stage) throws Exception {
    URL url = getClass().getResource("FXMLDocument.fxml");
    System.out.println(url == null);
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

and somehow the URL is null. Line 25 is the Parent root = [...] part. The .fxml document has the exact same name as the Strings tell and is in the same package as the main application class. I didn't change anything, just the null check for the URL. I googled for a long time, but I couldn't find anything regarding this error.

IDE with project

Upvotes: 1

Views: 2688

Answers (2)

devil_io
devil_io

Reputation: 191

This is sort of a temporary solution that works.

First copy the resource to the same directory as src folder.

So if your src folder path is C:app/src , then copy all resources to C :app

Run the application again.

Upvotes: 0

Matthias Brück
Matthias Brück

Reputation: 204

Ok, I found the error, see here. The error is differnent but the solution is the same. I found a broken reference to dist.jar in my project settings. As the answered question in the link states I had to CLEAN and build the project for the first build. The broken reference got fixed by that. I deleted all already compiled files and now the compiler does everything needed and compiles the whole project.

Upvotes: 1

Related Questions