1

Why does this error happen?

Here is the 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>

Fist, i think that fx:controller="PackageName.ClassName" Examine a Screen.fxml many times, But all of that Okay.

Second, Examine a fxml Path:

Package Explorer

Scribe error code below

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$2(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)

2 Answers 2

1

When you invoke an FXML file via getClass().getRessource(path), you must provide a relative path starting from the location of your current class.

I would recommend you:

  1. To put Screen.fxml inside the ui folder

  2. Load it with the instruction: FXMLLoader.load(Main.class.getResource("../ui/Screen.fxml"));.

Sign up to request clarification or add additional context in comments.

Comments

0

I was facing a similar issue, and similar to what @0009laH has said, I put all the files Main.java , Hhlee.java , Screen.fxml in one package, that is in the App/src/application. That way, you won't have to specify the relative path every time, and you can just directly use this:

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);
    }
}

1 Comment

Thank for your help. i am appricaite your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.