2

i've tried anything else i found on stack stackoverflow and i really dont get it why this doesn't work. I won't show you the code of my application that is not working, because it isn't working even with the example project. So here is the problem:

When i create new JavaFX Application with the sample code that gives button which prints hello world after clicked, this works when i run this as a desktop application and when i build this and start in browser. This works perfectly as desktop and as browser application

But when i create new JavaFX FXML Application which is almost the same as above but stage is defined by fxml and css not byte he code. This one works perfectly as windows application but doesnt work as a browser application

java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
    at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.NullPointerException: Location is required.
    at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
    ... 11 more

here is the code of working application:

public class JavaFXApplication4 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

and this application doesnt work in browser and throws exception:

public class JavaFXApplication3 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Can someone help me?

Sam Orozco
  • 1,258
  • 1
  • 13
  • 27
user3590388
  • 55
  • 1
  • 6

4 Answers4

7

The problem is in this line:

Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

It is pretty clear that the load method is actually being called with a null argument. And that happens because getResource("/sample/sample.fxml") cannot file that resource.

The resource is missing (or has the wrong path) on the runtime classpath.

Source

Community
  • 1
  • 1
Jan
  • 205
  • 3
  • 9
0

The problem has to do with signing but I do not exactly understand what that means. Then I searched for how to run a JavaFX application in a browser with netbeans and found this: http://docs.oracle.com/javafx/2/fxml_get_started/fxml_deployment.htm

In netbeans 8.2 in the project properties under Build > Deployment there is a check-box:

Request unrestricted access (Enable signing)

With this it works. Good luck.

jHilscher
  • 1,810
  • 2
  • 25
  • 29
toffi5
  • 1
0

In some case, if you using maven you should move all your fxml files to the resources directory and use a relative address from there.

If you want to use stage.setScene() function inside a controller class, it's better to use:

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
Reza-S4
  • 1,042
  • 1
  • 18
  • 36
-2

if u are using different packages then cut that file from other package and paste it in main package. And then try to run . I have tried that approach and its working fine now .

Dinesh
  • 1