JavaFX的8个问题与FXML装载舞台

问题描述:

我试图加载我FXML文件阶段的JavaFX 8 这里是我的更新代码JavaFX的8个问题与FXML装载舞台

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:合作ntroller到我的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 
+0

应该是'MyViewPart.class.getResource(“/ bthesis/views/test.fxml”)'。此外,在运行应用程序时,您必须确保它实际上包含在类路径中。 – fabian

+0

添加我的fxml文件来构建补丁,但现在我的Eclipse告诉我,我有错误,不知道哪个。我只是不能启动我的程序 – LucasPG

+0

你的窗口变量是否被初始化?如果这发生在'window.setScene(s1);'我敢打赌,这是因为你没有初始化变量 –

您需要同时使用setLocation方法

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

如果这无助于改变你这样的路径。创建一个java类,例如助手在视图中包,然后使用它作为参考

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

很遗憾没有帮助 – LucasPG

+0

@LucasPG看我的编辑 –

+0

还是没有:( – LucasPG

我认为你必须混乱设定路径。 我解释一下。

If you project directory structure is like 

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

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

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

我正在使用AnchorPane FXML文件类型,而且我正在做类似'GuiStage =(AnchorPane)FXMLLoader.load(MyViewPart。 class.getResource(“/ bthesis/views/test.fxml”));'仍然不知道如何解决这个问题,你的代码不工作太我的朋友:( – LucasPG

+0

你可以发布你的错误StackTrace – CTN

+0

已更新整个帖子 – LucasPG