在 fxml 文件 JAVA 代码中使用控制器时出错

getting error when using controller in fxml file JAVA Code

为什么会出现这个错误?

代码如下:

Main.java

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Screen.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Hhlee.java

public class Hhlee implements Initializable {
    @FXML private Button btn1, btn2, btn3;
    
    @Override
    public void initialize(URL location , ResourceBundle resources) {
        btn1.setOnAction(event -> handleBtn1Action(event));
        btn2.setOnAction(event -> handleBtn2Action(event));
        btn2.setOnAction(event -> handleBtn3Action(event));
    }
    
    public void handleBtn1Action(ActionEvent event) {
        System.out.println("버튼1 클릭");
    }
    
    public void handleBtn2Action(ActionEvent event) {
        System.out.println("버튼2 클릭");
    }
    
    public void handleBtn3Action(ActionEvent event) {
        System.out.println("버튼3 클릭");
    }
}

Screen.fxml

<?xml version="1.0" encoding="UTF-8"?>

<HBox xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1"
        prefWidth="300.0" hgap="10.0" vgap="10.0"
        fx:controller="application.Hhlee"
        prefHeight="50.0" prefWidth="200.0" alignment="CENTER" spacing="20.0">
    <Button fx:id="btn1" text="버튼1"/>
    <Button fx:id="btn2" text="버튼2"/>
    <Button fx:id="btn3" text="버튼3"/>
</HBox>

拳头,我觉得fx:controller="PackageName.ClassName" 检查一个Screen.fxml很多次,但是都还好。

其次,检查一个 fxml 路径:

在下面记下错误代码

Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
        Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:831)
    Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)

当您通过 getClass().getRessource(path) 调用 FXML 文件时,您必须提供从当前 class.

的位置开始的相对路径

我会推荐你​​:

  1. Screen.fxml放入ui文件夹

  2. 用指令加载它:FXMLLoader.load(Main.class.getResource("../ui/Screen.fxml"));.

我遇到了类似的问题,与@0009laH 所说的类似,我将所有文件 Main.javaHhlee.javaScreen.fxml 放在一个包中,即App/src/application。这样就不用每次都指定相对路径了,直接用这个就可以了:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
         FXMLLoader loader=new FXMLLoader(getClass().getResource("Screen.fxml"));
         Parent root=(Parent) loader.load();
         Scene scene = new Scene(root);
         primaryStage.setScene(scene);
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}