从另一个fxml文件的控制器中读取fxml文件时出现Null Point异常

时间:2015-05-07 14:19:13

标签: javafx-2 fxmlloader

我面临同样的问题!

我正在向Button添加ImageView,并使用SceneBuilder将图像路径添加到FXML文件中的ImageView。当我通过Eclipse IDE运行它时工作正常,但是当作为jar文件运行时却没有。

从Button中删除ImageView后,即使以jar格式运行,它也能正常工作。

但是,现在我在不同情况下遇到了同样的问题!

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

  1. MainWindow(边框窗格) - 包含一个菜单栏和工具栏,添加到边框窗格的顶部位置。
  2. CenterWindow(主播窗格)
  3. ButtomWindow(Anchor Pane)
  4. 在MainWindow的控制器中,我试图添加不同fxml文件中的CenterWindow和ButtomWindow。

    但是,NullPointerException出现在我尝试加载fxml文件的行的MainWindow控制器中。它说在例外中需要位置。 我已经提取了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
    

1 个答案:

答案 0 :(得分:1)

我试图使用Data加载fxml文件,这从jar运行时会出现异常。 而是使用getClass().getResource(../view/Output.fxml")ClassLoader.getSystemResource("com/example/app/view/Output.fxml") 将解决问题。

相关问题