2015-12-20 128 views
2

我目前正在將我的匆忙製作的JavaFX應用程序佈局從Java遷移到FXML,所以我可以單獨擔心樣式,視圖,控制器和模型,但我似乎無法運行簡單你好世界應用程序。爲什麼我無法啓動這個簡單的FXML Hello World應用程序?

我得到以下錯誤:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 

這裏是View.fxml文件

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml" fx:controller="calend.ae.Controller"> 

    <Label text="I love bacon"/> 
    <Button text="Submit"/> 

</VBox> 

和主類

package calend.ae; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Model extends Application { 

    public static void main(String[]args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stg) throws Exception{ 
     Parent tabs = FXMLLoader.load(getClass().getResource("View.fxml")); 
     stg.setTitle("Hello World"); 
     stg.setScene(new Scene(tabs, 450, 250)); 
     stg.show(); 
    } 
} 

你可以找到源全部代碼here。我只是想知道爲什麼它不跑,這裏上下文是實際application

以下是完整的堆棧跟蹤:使用maven或螞蟻

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: 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 calend.ae.Model.start(Model.java:17) 
    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 calend.ae.Model 
Picked up _JAVA_OPTIONS: -Xmx512M 
+0

安置自己的代碼在這裏不是在外部網站 – Reimeus

+0

還發布完整的堆棧跟蹤。 –

+0

@James_D貼,謝謝。 – Zunon

回答

1

如果你使用ant的FXML應該在同一個dir其中Model.java是

+0

我不知道是哪個,我在默認設置上使用了eclipse(大部分),但是我在根目錄和Model.java旁都嘗試過,沒有任何效果。 – Zunon

+0

如果它的maven你可能有2個文件夾在你的src目錄。 「java」和「resources」在資源內部創建一個fxml文件夾,並將fxml文件放在那裏。 –

+0

嗯,它現在可以工作,github桌面程序把fxml文件放在model.java旁邊,我想它會在同步之前從那裏刪除它。謝謝 – Zunon

相關問題