java - JavaFX java.lang.NullPointerException:加载 .fxml 时需要位置(问题不是 getClassloader)
问题描述
对于 Uni,我必须创建一个带有 GUI 的程序。在创建了所有逻辑和 fxml 文档以及我尝试运行程序但每次都遇到相同的问题后,似乎 jfx 无法加载 fxml 文档。我没有通过 google/Stackoverflow 找到我的问题的任何解决方案,最常见的问题是他们忘记了 .getClassloader() 这不是我的问题。当运行主类本身时,jfx 模块不会被加载,作为一种解决方法,我使用另一个主调用真正的主。
运行 workaroundmain 时,出现以下异常:
Exception in Application start method
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:830)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at application.GUI.Main.mainWindow(Main.java:38)
at application.GUI.Main.start(Main.java:31)
抛出异常的代码(FXMLloader.load(MainWindow)):
private static Stage primaryStage;
private static URL MainWindow,TableView,ChoiceWindow;
@Override
public void start(Stage primaryStage)
{
MainWindow = getClass().getClassLoader().getResource("MainWindow.fxml");
TableView = getClass().getClassLoader().getResource("PersonView.fxml");
ChoiceWindow = getClass().getClassLoader().getResource("Ludger.fxml");
Main.primaryStage = primaryStage;
mainWindow();
}
public void mainWindow()
{
try {
Parent parent = FXMLLoader.load(MainWindow);
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
我试过:删除/添加文件,重命名 MainWindow.fxml,用整个路径交换 MainWindow.fxml,可能还有很多没有改变任何东西。在这一点上,我不知道要尝试什么来解决问题。
解决方案
推荐阅读
- python - 在 Windows 中设置 python 3 环境很简单:只需从 python.org 安装,或者真的会从 miniconda 或类似工具中受益吗?
- javascript - 转换截断的日期时间字符串
- python - 使用自定义函数创建 n*n DataFrame
- angular - Angular 单元测试 - 未使用模拟服务
- android - Ionic 3 本机短信插件无法在我的设备上运行
- python - Python 3 Selenium Webdriver 如何将重复动作变成功能?
- excel - 将数组输出到范围的快速方法
- wordpress - Wordpress 数据库连接错误...寻找旧用户名?
- excel - 取消合并列时过程太大
- java - SparkSql 和正则表达式