即使我认为设置正确,FXML getLocation()也会返回null

时间:2020-05-02 21:32:20

标签: java javafx fxml

当我尝试将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”时,该位置开始工作,有人可以帮忙为什么这会导致问题吗?

0 个答案:

没有答案