IDEA - 无法加载java资源

时间:2015-03-13 19:16:13

标签: java javafx resources

我正在学习JavaFX个应用程序,似乎无法找到fxml资源文件。 我正在运行Intellij Idea IDE 14,我的project结构是:

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( .....

看起来资源未找到。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

尝试在路径前添加/。它从包fr.hznteam.contacts.view

加载资源
root = FXMLLoader.load(getClass().getResource("/fr/hznteam/contacts/view/ContactsView.fxml"));