Javafx 2 从另一个fxml文件的控制器加载fxml文件时出现空点异常

Javafx 2 从另一个fxml文件的控制器加载fxml文件时出现空点异常,javafx-2,fxmlloader,Javafx 2,Fxmlloader,我也面临同样的问题 我正在使用SceneBuilder将ImageView添加到按钮,并将图像路径添加到FXML文件中的ImageView。当我通过EclipseIDE运行它时,它工作得很好,但当作为jar文件运行时,它就不工作了 从按钮中删除ImageView后,即使作为jar运行,它也可以正常工作 但是,现在我在不同的场景中遇到了相同的问题 我使用3个FXML文件来构建整个窗口 主窗口(边框窗格)——包含添加到边框窗格顶部位置的菜单栏和工具栏 中心窗口(锚定窗格) 按钮窗口(锚定窗格) 在主

我也面临同样的问题

我正在使用SceneBuilder将ImageView添加到按钮,并将图像路径添加到FXML文件中的ImageView。当我通过EclipseIDE运行它时,它工作得很好,但当作为jar文件运行时,它就不工作了

从按钮中删除ImageView后,即使作为jar运行,它也可以正常工作

但是,现在我在不同的场景中遇到了相同的问题

我使用3个FXML文件来构建整个窗口

  • 主窗口(边框窗格)——包含添加到边框窗格顶部位置的菜单栏和工具栏
  • 中心窗口(锚定窗格)
  • 按钮窗口(锚定窗格)
  • 在主窗口的控制器中,我试图添加CenterWindow和ButtomWindow,它们位于不同的fxml文件中

    但是,
    NullPointerException
    发生在我试图加载fxml文件的行的主窗口控制器中。它说在异常中需要位置。 我已经提取了jar文件并检查,fxml文件在jar中。 有人能帮忙吗

    包结构:

    com.example.app -- contains Main.java which has main() method
    com.example.app.controller -- MainWindowController.java, CenterWindowController.java, ButtomWindowController.java
    com.example.app.view -- MainWindow.fxml, CenterWindow.fxml, ButtomWindow
    
    在Main.java中,我正在加载MainWindow.fxml,它可以正常加载。然后在MainWindowController.java中,我尝试加载CenterWindow.fxml&ButtomWindow,这会给出异常

    异常日志:

    javafx.fxml.LoadException:
    file:/C:/Users/a27490989/Documents/NetBeansProjects/AirbusDS/dist/AirbusDS.jar!/
    ds/airbus/simulator/view/MainWindow.fxml
    
        at javafx.fxml.FXMLLoader.constructLoadException(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.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 ds.airbus.simulator.Main.start(Main.java:22)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$51/747183799.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$44/584634336.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$47/94326726.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$45/501263526.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$37/96639997.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 ds.airbus.simulator.controller.MainWindowController.initOutputView(MainWindowController.java:214)
        at ds.airbus.simulator.controller.MainWindowController.initialize(MainWindowController.java:204)
        ... 23 more
    

    我试图使用
    getClass().getResource(../view/Output.fxml”)
    加载fxml文件,从jar运行时出现异常。 而是使用
    ClassLoader.getSystemResource(“com/example/app/view/Output.fxml”)
    getClass().getResource(“/com/example/app/view/Output.fxml”)

    将解决问题。

    getClass().getResource(“../view/Output.fxml”)在MainWindowController.java中从JAR文件运行时提供空值。请尝试
    getClass().getResource(“/com/example/app/view/Output.fxml”)
    @ItachiUchiha,您的建议有效..:)谢谢!