2016-04-22 109 views
1

我在Eclipse中編寫代碼,並試圖使用javafx和fxml ad CSS爲我的Java類中的最終項目創建MAd Libs遊戲。我已經安裝了gluon的最新版本場景生成器,以及來自java的最新JRE。這是我的代碼有Eclipse不會讀取/運行fxml文件

package application; 
    import java.io.IOException; 
    import java.util.ResourceBundle; 

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

    public class Main extends Application{ 
    public static void main(String[] args) { 
    launch(args); 
} 

@Override 
public void start(Stage primaryStage) throws IOException { 

     Parent root = FXMLLoader.load(getClass().getResource("scenebuilder.fxml")); 
     Scene scene1 = new Scene(root); 
     primaryStage.setTitle("Shane Ramos Final Project"); 
     primaryStage.setScene(scene1); 
     primaryStage.show(); 
     } 
    } 

每當我嘗試運行它(已調用它在我的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: 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 application.Main.start(Main.java:21) 
    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 application.Main 

我已經試過所有我能想到的解決這一問題,並在上​​午共損失。任何幫助表示讚賞。謝謝!

+0

與main相同的包中有scenebuilder.fmxl嗎? – jns

+0

是的,我相信它是 –

回答

0

你必須先設置一個位置:

例如:

private Parent root;

...

FXMLLoader loader = new FXMLLoader(); loader.setLocation(YourClass.class.getResource("yourFile.fxml"));

root = (Parent) loader.load();

並確保您的fxml文件位於正確的文件夾中。

根據你的代碼,你的fxml文件和你的Main.java應該在系統的同一個文件夾中。