HOWTO · Java
JavaFX의 InvocationTargetException
이 자습서는 JavaFX에서 호출 대상 예외를 해결하는 방법을 보여줍니다.
이 페이지의 내용
이 튜토리얼은 JavaFX에서 InvocationTargetException을 해결하는 방법을 보여줍니다.
JavaFX에서 InvocationTargetException 수정
InvocationTargetException 예외는 JavaFX를 사용할 때 발생합니다. 이 예외가 발생하는 이유는 다음과 같습니다.
- FXML 파일의 경로 이름 시작 부분에 슬래시를 쓰거나 지정된 경로가 올바르지 않거나 완전하지 않은 경우.
- 동적 루트를 사용하려고 하지만 FXML 로더에 루트를 설정하지 않은 경우.
- JavaFX 모듈을 놓쳤을 때.
JavaFX에서 InvocationTargetException을 발생시키는 예제를 시도해 봅시다.
package delftstack;
import java.io.File;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Example extends Application {
@Override
public void start(Stage DemoStage) throws Exception {
System.out.println("Start()");
Parent DemoRoot = FXMLLoader.load(getClass().getResource("delftstack.fxml"));
DemoStage.setTitle("Demo");
Scene DemoScene = new Scene(DemoRoot, 200, 150);
DemoStage.setScene(DemoScene);
DemoStage.show();
}
}
delftstack.fxml 파일은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ScrollPane layoutX="178.0" layoutY="68.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<TableView prefHeight="350.0" prefWidth="590.0">
<columns>
<TableColumn prefWidth="130.0" text="First Name" />
<TableColumn prefWidth="130.0" text="Last Name" />
<TableColumn prefWidth="330.0" text="ID" />
</columns>
</TableView>
</content>
</ScrollPane>
</children>
</AnchorPane>
위의 코드는 delftstack.fxml 파일을 찾을 수 없기 때문에 InvocationTargetException을 발생시킵니다. 출력 참조:
Start()
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at delftstack.Example.start(Example.java:15)
at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
이제 delftstack.fxml 파일의 절대 경로를 지정하여 해결하려고 하면 문제가 해결될 수 있지만 일부 시스템에서는 여전히 동일한 문제가 발생할 수 있습니다. 이 오류에 대한 최상의 솔루션은 다음과 같습니다.
- 클래스의 동일한 디렉토리에
delftstack.fxml파일을 복사합니다. 예를 들어 클래스 파일이delftstack패키지에 있는 경우 FXML 파일을project/src/delftstack폴더에 복사해야 합니다. - IDE에서 프로젝트를 새로 고칩니다. 예를 들어 Eclipse에서 프로젝트 이름을 마우스 오른쪽 버튼으로 클릭하고 새로 고침을 선택합니다.
- 같은 프로그램을 실행하면 오류가 해결됩니다.
위의 솔루션을 따른 후의 출력은 다음과 같습니다.