首页 > 解决方案 > FXML getLocation() 返回 null 即使我认为我设置正确

问题描述

当我尝试将我的 FXML 工作表链接到我的主类时,我收到以下错误

Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2463)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2439)
        at com.nandha.client.start(client.java:28)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
        ... 1 more

这是我的启动方法

        stage.setTitle("Client");
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/Main.fxml"));
        System.out.println(loader.getLocation()); //Prints Null
        VBox box1 = loader.<VBox>load(); //Code Breaks Here

        Scene scene = new Scene(box1, 300, 150);
        stage.setScene(scene);
        stage.show();
    }

这是我的 FXML 文件

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.Button?>
<VBox fx:controller="com.nandha.Controller" xmlns:fx="http://javafx.com/fxml">
        <Text fx:id="status" text="Not Connected"/>
        <Button  fx:id="connect" text="Connect" onAction="#connectToServer"/>
        <Button fx:id="buttonNew" text = "Add New Patient" onAction="#openCreateNewPage"/>
        <Button fx:id="buttonUpdate" text="Existing Patient"/>
</VBox>

FXML 文件、主类和对应的类都在同一个文件夹和包中,src/com/nandha/。我尝试过在 main 前面使用和不使用斜杠,并添加包名。提前致谢!

编辑:由于某种原因,当我从 xml 文件中删除 fx:controller="com.nandha.Controller" 时,该位置开始工作,有人可以帮助解释为什么这会导致问题吗?

标签: javajavafxfxml

解决方案


推荐阅读