NullPointerException 从 javafx 小程序加载 fxml 文件 [英] NullPointerException loading fxml file from javafx applet

查看:45
本文介绍了NullPointerException 从 javafx 小程序加载 fxml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 IntelliJ Idea 创建 javafx 小程序.构建后我得到三个文件:.jar、.jnlp 和 .html.如果我启动 jar 一切正常,但是如果我尝试使用 jnlp 或 html 运行应用程序,则会引发异常:

I'm trying to create javafx applet, using IntelliJ Idea. After building I get three files: .jar, .jnlp and .html. If I launch jar all works fine, but if I try to run app using jnlp or html it throws exception:

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 sample.Main.start(Main.java:14)
    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$45/31918841.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$44/13936151.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$34/10055096.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$45/31918841.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$44/13936151.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$34/10055096.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 sample.Main.start(Main.java:14)
    ... 11 more

为什么会这样?Jar 正好包含位于指定路径的所需 fxml.

Why is it so? Jar exactly contains needed fxml located at the specified path.

Java:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button layoutX="173.0" layoutY="188.0" prefHeight="25.0" prefWidth="255.0" text="Button" />
   </children>
</AnchorPane>

我做错了什么?请帮忙.

What am I doing wrong? Help please.

推荐答案

这是一个安全问题.FXML 文件被认为是从客户端的文件系统加载的,但 Java 安全管理器不容忍user.dir, read"权限.

This is a problem of security. The FXML file is considered as to be loaded from the filesystem of the client, but the "user.dir, read" permission is not tolerated by the Java security manager.

然而,这是一个JavaFX小程序的签名问题.一旦签署,加载工作良好.此处示例:http://web.univ-pau.fr/~barbier/PauWare/My_device/dist/My_device_JavaFX.html

However, it is a problem of signing JavaFX applets. Once signed, the loading works well. Example here: http://web.univ-pau.fr/~barbier/PauWare/My_device/dist/My_device_JavaFX.html

顺便说一句,JavaFX 应用程序没有问题(例如,http://www.franckbarbier.com/PauWare/Programmable_thermostat/)代码中的自包含小部件(不加载 FXML 文件).干杯!!

BTW, there is no problem with JavaFX applications (eg, http://www.franckbarbier.com/PauWare/Programmable_thermostat/) that have self-contained widgets in the code (no load of FXML file). Cheers!!

这篇关于NullPointerException 从 javafx 小程序加载 fxml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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