java - java.lang.NullPointerException:需要位置。javafx
问题描述
我正在尝试从路径src/interfaces/panels/中的 fxml 文件创建一个 Scene 类,该文件名为StartUp.fxml,但出现此错误:
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)
at tienda.TIENDA.start(TIENDA.java:30)
public void start(Stage stage) throws Exception {
window=stage;
String path = new File("src/interfaces/panels/StartUp.fxml").getAbsolutePath();
System.out.println(path);//I added this just in case
Parent root = FXMLLoader.load(getClass().getResource("src/interfaces/panels/StartUp.fxml"));//this is the problem
su=new Scene(root);
window.setScene(su);
window.show();
}
编辑:这是解决方案 TIENDA是类,路径是相对于类的
public void start(Stage stage) throws Exception {
window=stage;
var resource = TIENDA.class.getResource("../src/interfaces/panels/StartUp.fxml");//HERE
System.out.println(resource);
Parent root = FXMLLoader.load(resource);
su=new Scene(root);
window.setScene(su);
window.show();
}
解决方案
推荐阅读
- php - php 创建具有额外值的第三个数组,其中键与两个现有数组匹配
- java - 飞碟设置横向时创建空白页面?
- r - %<>% 操作的含义
- .net - .NET HotChocolate 泛型类型分页
- android - vkEnumeratePhysicalDevices 在使用 adb shell 执行的 android 命令行程序中返回 0
- vue.js - Vue JS 组件数据没有返回到模板
- python - CDK S3-Assets 参数必须有值
- android - 不支持报警管理器中的其他插件?Flutter 不支持某些任务的后台执行(如果我们杀死了应用程序)?
- babeljs - 如何使用 Babel 要求项目范围之外的文件?
- microsoft-graph-api - 如何使用 Graph Api 检索 OneDrive 中的所有目录和子目录?