JavaFX web部署:FXMLLoader NullPointerException

JavaFX web部署:FXMLLoader NullPointerException,java,intellij-idea,javafx,fxml,fxmlloader,Java,Intellij Idea,Javafx,Fxml,Fxmlloader,我想尝试JavaFX web浏览器部署,所以我使用IntelliJ community edition 14.0.2 JavaFX应用程序模板启动了一个非常简单的测试项目。代码如下:(NO插件、外部库或maven) Main.java @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.f

我想尝试JavaFX web浏览器部署,所以我使用IntelliJ community edition 14.0.2 JavaFX应用程序模板启动了一个非常简单的测试项目。代码如下:(NO插件、外部库或maven)

Main.java

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}
Controller.java(附加到sample.fxml)

sample.fxml(布局文件)

相关的代码行似乎是

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
然而,当作为单个独立JAR构建和打开时,这行代码可以正常工作!
知道错误发生的原因或如何修复吗?

这个错误有点神秘,但可以理解。您将获得一个带有本地化消息“Location is required”的空指针,该消息表示您没有设置查找FXML的位置


getClass().getResource(“sample.fxml”)返回null,因此FXMLLoader会抛出null指针。检查fxml的路径,您可能希望在它前面加一个“/”,比如“/sample.fxml”。

这并没有解决问题:(正如我在原始帖子中所说的,独立JAR工作得很好,甚至可以不加前缀“/”.抛出错误的只是JavaFX工件…然后看起来像是类路径问题,您可以检查jar内容,看看sample.fxml是否在其中?
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
      prefHeight="400.0" prefWidth="600.0" spacing="20.0" xmlns="http://javafx.com/javafx/8"
      xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
   </padding>
   <TextField fx:id="textField" onAction="#setLabelText"/>
   <Label fx:id="label" text="Label">
      <font>
         <Font size="96.0"/>
      </font>
   </Label>
</VBox>
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:13)
    at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1838209255.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/1604839423.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$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/982109627.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$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1838209255.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/1604839423.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$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/982109627.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:13)
    ... 11 more
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));