2015-03-13 155 views
0

我在學習JavaFX應用程序,似乎找不到fxml資源文件。 我運行Intellij Idea IDE 14,我project結構爲:IDEA - 無法加載Java資源

enter image description here

ContactViews.fxml文件位於:在設置/編譯器/資源模式src/fr/hznteam/contacts/view/

:!? .java;!? .form;!? .class;!? .groovy;!? .scala;!? .flex;!? .kt;!? !?。 .clj; * FXML

運行此代碼:

public class ContactBoard extends Application { 

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

    @Override 
    public void start(Stage primaryStage) { 

     Parent root; 

     try {; 
      root = FXMLLoader.load(getClass().getResource("/fr/hznteam/contacts/view/ContactsView.fxml")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return; 
     } 

     Scene scene = new Scene(root); 
     primaryStage.setScene(scene); 
     primaryStage.sizeToScene(); 
     primaryStage.show(); 
    } 


} 

我面臨以下錯誤:

Exception in Application start method 
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) 
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47) 
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException: Location is required. 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2825) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771) 
    at fr.hznteam.contacts.view.ContactBoard.start(ContactBoard.java:28) 
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) 
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219) 
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182) 
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76) 
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 
    at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89) 

Line 28 : root = FXMLLoader.load(getClass().getResource(..... 

它看起來像資源未找到。

任何人都可以幫助我嗎?

回答

2

嘗試在路徑前添加/。它加載資源從包fr.hznteam.contacts.view

root = FXMLLoader.load(getClass().getResource("/fr/hznteam/contacts/view/ContactsView.fxml")); 
+0

是抱歉,shuld是根= FXMLLoader.load(的getClass()getClassLoader()的getResource( 「FR/hznteam /聯繫人/視圖/ ContactsView.fxml」)); 或root = FXMLLoader.load(getClass()。getResource(「/ fr/hznteam/contacts/view/ContactsView.fxml」));但它不起作用 – 2015-03-13 20:57:34

+0

檢查拼寫錯誤或尾隨空格,否則休息就好了。 – ItachiUchiha 2015-03-14 09:22:08