提问人:Hopelessdecoy 提问时间:8/19/2022 更新时间:8/19/2022 访问量:140
JavaFX 无法加载我的 FXML,返回空指针异常 [duplicate]
JavaFX cannot load my FXML, returning null pointer exception [duplicate]
问:
我不确定错误是什么,我正在将家庭项目从 Swing GUI 转换为 JavaFX,使用场景构建器并且遇到了困难。请帮忙? 有关我的错误和项目结构的详细信息如下:
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:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
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 [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at [email protected]/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.IllegalStateException: Location is not set.
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
at [email protected]/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
at testify.Runtime.App.start(App.java:22)
at [email protected]/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at [email protected]/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at [email protected]/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application testify.Runtime.App
Process finished with exit code 1
这是我的项目树:
这是我的主要代码:
package testify.Runtime;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader= new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = (Parent) loader.load();
Scene home = new Scene(root);
primaryStage.setTitle("Testify");
primaryStage.setScene(home);
primaryStage.show();
}
}
由于某种原因忽略这一行,如果没有在堆栈溢出上写一段关于它的段落,我无法发布“主要是代码”问题,我不确定为什么......这里以前从未有过这个问题。
答:
2赞
Naresh Sharma
8/19/2022
#1
只需在 fxml 文件的名称前加上前缀/
FXMLLoader(getClass().getResource("/main.fxml"))
当您不使用正斜杠时,您的 URL 将形成为 而您的资源位于资源文件夹中testify/Runtime/main.fxml
评论
0赞
Hopelessdecoy
8/19/2022
谢谢,我知道这是一件小事
评论