为什么我的主类中有异常,我认为处理程序中的每个异常

问题描述 投票:0回答:1

目前我有两个包:metier和view模型包是空的。 metier包含我的主应用程序:

private Stage primaryStage;
private BorderPane rootLayout;

@Overrid
     public void start(Stage primaryStage) {
            this.primaryStage = primaryStage;
            this.primaryStage.setTitle("AddressApp");

            initRootLayout();

            showPersonOverview();
        }

初始化根布局:

        public void initRootLayout() {
            try {
                // Load root layout from fxml file.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(MainApp.class.getResource("/view/RootLayout.fxml"));
                rootLayout = (BorderPane) loader.load();

显示包含根布局的场景:

                Scene scene = new Scene(rootLayout);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

显示根布局内的人员概述:

        public void showPersonOverview() {
            try {
                // Load person overview.
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(MainApp.class.getResource("/view/PersonOverview.fxml"));
                AnchorPane personOverview = (AnchorPane) loader.load();

将人员概述设置为根布局的中心:

                rootLayout.setCenter(personOverview);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

返回主阶段:

        public Stage getPrimaryStage() {
            return primaryStage;
        }

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

view包含两个fxml文件PersonOverView.fxml和RootLayout.fxml所有工作都是使用场景构建器。

整个堆栈跟踪:

    Executing Z:\WorkJava\BonPlansApp\dist\run139321931\BonPlanApp.jar using platform C:\Program Files\Java\jdk1.8.0_201\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at metier.MainApp.initRootLayout(MainApp.java:41)
    at metier.MainApp.start(MainApp.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application metier.MainApp
Java Result: 1
Deleting directory Z:\WorkJava\BonPlansApp\dist\run139321931
jfxsa-run:
BUILD SUCCESSFUL (total time: 2 seconds)
javafx scenebuilder
1个回答
0
投票

这个问题在StackTrace的这一行描述:

Caused by: java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane

这来自加载你的RootLayout.fxml文件的行:

rootLayout = (BorderPane) loader.load();

基本上,该行的作用是告诉FXMLLoader加载RootLayout.fxml文件并将根元素作为BorderPane返回。你没有发布你的FXML文件,但我怀疑第一个元素是<AnchorPane...,而不是<BorderPane...

要么将rootLayout类中的Main声明为AnchorPane,要么将FXML文件的根元素更改为BorderPane

© www.soinside.com 2019 - 2024. All rights reserved.