JavaFX 8 FXML加载作为阶段的问题

时间:2016-10-22 11:33:18

标签: java javafx fxml

我正在尝试将我的FXML文件加载为JavaFX 8中的阶段。 这是我的更新代码

Stage window;
private AnchorPane GuiStage;

    public void showMainView() throws IOException
    {
    try{
       FXMLLoader loader = new   MLLoader(MyViewPart.class.getResource("/bthesis/views/test.fxml"));
       AnchorPane GuiStage = loader.load();
       Scene s1 = new Scene(GuiStage);
       window.setScene(s1);
       window.show();
    }catch(Exception e){
        System.out.println(e);
    }
    }

我的FXML文件的完整补丁是

C:\Users\Łukasz\workspace\BThesis\src\bthesis\views\test.fxml

包名称

bthesis.views

我将fx:controller添加到我的FXML文件中,现在我得到了空指针错误。完整的错误跟踪。

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at bthesis.MyViewPart.showMainView(MyViewPart.java:67)
    at bthesis.MyViewPart.start(MyViewPart.java:102)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(Unknown Source)
    ... 1 more
Exception running application bthesis.MyViewPart

2 个答案:

答案 0 :(得分:0)

使用 setLocation 方法

时,您需要更改此路径
 loader.setLocation(MyViewPart.class.getResource("views/test.fxml"));

如果这没有帮助。创建一个java类,例如在views包中 Helper 然后将其引用用作

  loader.setLocation(Helper.class.getResource("test.fxml"));

答案 1 :(得分:0)

我认为你设置路径有困惑。 我解释一下。

If you project directory structure is like

    src
    .
    .
    .----bthesis
         .
         .
         .------views
                .
                .
                .test.fxml

然后你必须像这样设置位置路径。

loader.setLocation(MyViewPart.class.getResource("/bthesis/views/test.fxml"));