首先,我绝对不是母语人士所以我希望我能在这篇文章中找到正确的内容:D

那有什么问题呢?当我尝试使用几个不同的“窗口”构建一个javaFX应用程序时 . 问题是当我尝试在MainController类中生成一个initialize()函数时 . 此函数应该使用登录名和密码字段加载第一个窗口 .

问题只有在我在MainController类的initialize()方法中加入一些逻辑才能加载另一个Window时 . 对我来说很奇怪,因为根据控制台的主要问题是编译器找不到构建主窗口的.fxml文件,而不是第二个(由initialize()方法构建的 .

如果有人知道什么似乎是问题?

错误是:

找不到:.file:/ C:/Users/ADMIN/workspace%20FX/SchoolList/target/classes/fxml/AppMainScreen.fxml //这是我的try / catch块的输出,.fxml文件用于当然 . 路径和名称是正确的 . 在javafx.fxml.FXMLLoader的javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)的javafx.fxml.LoadException:/C:/Users/ADMIN/workspace%20FX/SchoolList/target/classes/fxml/AppMainScreen.fxml .loadImpl(FXMLLoader.java:2579)位于javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)的javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)位于pl.mati.schoolList.app.SchoolListApp . start(SchoolListApp.java:28)at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 162(LauncherImpl.java:863)at com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 175(PlatformImpl.java: 326)at com.sun.javafx.application.PlatformImpl.lambda $ null $ 173(PlatformImpl.java:295)at java.security.AccessController.doPrivileged(Native Method)at com.sun.javafx.application.PlatformImpl.lambda $ runLater $ com(PlatformImpl.java:294)位于com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at com . sun.glass.ui.win . java.lang.Thread.run(Thread.java:745)中的WinApplication.lambda $ null $ 148(WinApplication.java:191)引起:pl.mati.schoolList.controllers.MainController.initialize(MainController)中的java.lang.NullPointerException .java:73)在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)... 12更多异常在应用程序启动方法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位于sun.reflect的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)的com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)中的.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) sun.reflect.Delega上的.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)位于sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:767)的java.lang.reflect.Method.invoke(Method.java:498)中的tingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)引起:java.lang .RuntimeException:com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)中的应用程序启动方法中的异常,位于com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 155(LauncherImpl.java:182)at java.lang.Thread.run(Thread.java:745)引起:java.lang.NullPointerException:在javafx.scene.Scene的javafx.scene.Scene . (Scene.java:336)中,Root不能为null . (场景.java:194)在com.sun.javafx.application.LauncherImpl.lambda上的pl.mati.schoolList.app.SchoolListApp.start(SchoolListApp.java:42),com.sun的$ launchApplication1 $ 162(LauncherImpl.java:863) .javafx.application.PlatformImpl.lambda $ runAndWait $ 175(PlatformImpl.java:326)at com.sun.javafx.application.PlatformImpl.lambda $ null $ 173(PlatformImpl.java:295)at java.security.AccessController.do Com.sun.javafx.application.PlatformImpl.lambda上的特权(本地方法)$ runLater $ 174(PlatformImpl.java:294)com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)at com com.sun.glass.ui.win.WinApplication.lambda上的.sun.glass.ui.win.WinApplication._runLoop(Native Method)$ null $ 148(WinApplication.java:191)... 1更多异常运行应用程序pl . mati.schoolList.app.SchoolListApp

编译器无法加载fxml文件,这会在地方导致nullPointerException,其中应该注入StackPane(从fxml文件加载stackPane) .

这是我的类:我的主类,启动应用程序的地方 .

SchoolListApp.class

public class SchoolListApp extends Application{

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

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        StackPane mainStackPane = null;

        URL path = getClass().getResource("/fxml/AppMainScreen.fxml");

        try {
            loader.setLocation(path);
            mainStackPane = (StackPane) loader.load();
        }
         catch (IOException e) {
            System.out.println("Not found: " + path);
            e.printStackTrace();

        }
        catch (Exception e) {

            // Answer:
            e.getCause().printStackTrace();

        }
        MainController controller = loader.getController();
        Scene scene = new Scene(mainStackPane);  //here i got the Caused by: java.lang.NullPointerException: Root cannot be null. error//

        stage.setScene(scene);

        stage.setTitle("School List Manager Application");
        stage.show();

    }

}

主控制器类 . 如果initiliazie方法为空 - 编译器工作正常,它构建了第一个窗口 . 但是当我尝试构建另一个窗口时,我得到了错误 .

MainController.class

public class MainController implements Initializable{

    @FXML
    private StackPane mainStackPane;

    @FXML
    public void initialize(){

        FXMLLoader loader = new FXMLLoader();
        Pane pane = null;

        URL path;
        path = getClass().getResource("/fxml/HelloWindow.fxml");
        try {
            loader.setLocation(path);
            pane = loader.load();
        }
         catch (IOException e) {
            System.out.println("Not found: " + path);
            e.printStackTrace();

        }
        catch (Exception e) {

            // Answer:
            e.getCause().printStackTrace();

        }
        mainStackPane.getChildren().add(pane);

    }

    public MainController() {

    }