这个外汇申请有什么问题?

时间:2015-03-18 17:05:40

标签: javafx fxml

我想知道为什么这段代码错了,它给了我一个异常,GUI.fxml就在项目的根目录上。

public class MyApp extends Application{

        @Override
        public void start(Stage primaryStage) throws Exception {
            String location = "/GUI.fxml";
            Parent root = FXMLLoader.load(getClass().getResource(location));

            primaryStage.setScene(new Scene(root,300,450));
            primaryStage.setTitle("Minha Janela");
            primaryStage.show();
        }

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

已经搜索了很多,尚未找到解决方案。

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(Unknown Source)
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 g1.MyApp.start(MyApp.java:13)
    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.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    ... 1 more

1 个答案:

答案 0 :(得分:1)

FXML不应该在项目的根目录中,而应该在classpath中。尝试将fxml重新定位到源文件夹中。

对于常规项目,您有一个src文件夹。您也可以创建自己的自定义源文件夹。

对于maven项目,您可以尝试将它们保留在src/main/resources内。