JavaFX NullPointerException位置是必需的NetBeans [英] JavaFX NullPointerException Location is required NetBeans

查看:148
本文介绍了JavaFX NullPointerException位置是必需的NetBeans的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了我在堆栈stackoverflow上找到的任何其他东西,我真的不明白为什么这不起作用。
我不会向您展示我的应用程序的代码无法正常工作,因为它甚至不能用于示例项目。
所以这就是问题所在:

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:

当我创建新的JavaFX应用程序时,示例代码给出了单击后打印hello world的按钮,这在我运行时有效这是一个桌面应用程序,当我构建它并在浏览器中启动时。这完全适用于桌面和浏览器应用程序

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

但是当我创建新的JavaFX FXML应用程序时几乎与上面相同但阶段由fxml和css定义而不是字节他码。这个作为Windows应用程序完美地运行但不能用作浏览器应用程序

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);
    }

}

有人可以帮助我吗?

推荐答案

问题出在这一行:

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

很明显,实际上是使用null参数调用load方法。这是因为getResource(/ sample / sample.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

这篇关于JavaFX NullPointerException位置是必需的NetBeans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆