JavaFX FXMLLoader 上的 NullPointer 位置
NullPointer location on JavaFX FXMLLoader
我已经在 Whosebug 上阅读了所有关于此的问题,但我没有使用过有效的方法。我有一个基本的 fxml,它是在使用 Java 1.7.
时使用 JavaFX Scene Builder 1.1 构建的
我只想加载文件...但所有内容似乎都指向一个空位置,这意味着它无法找到它。我不明白为什么。我有 18 try/catches 来显示 18 种不同的工作可能性,但它找不到它。这些示例是从一些 Whosebug 问题中提取的 'valid accepted answers'。我在这里错过了什么?一切都会编译,所以我不认为我缺少 SDK 或重要的东西。
日志打印 1 到 18,并在第 18 位出现 NullPointerException try/catch,表明 NullPointer... location is required.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class OptionsToggleMenu extends Application {
public void launchTheThing(String... args){ //runs this on the Main application's main method.
launch(args);
}
@Override
public void start(Stage primaryStage) {
int failures = 0;
try{
Parent root1 = new Parent() {
};
try{
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(1);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("AdventureLibrary/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(2);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(3);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(4);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(5);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(6);
failures++;
}
try{
root1 = FXMLLoader.load(getClass().getResource("/AdventureLibrary/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(7);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/AdventureLibrary/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(8);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("/bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(9);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(10);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(11);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(12);
failures++;
}
try{
root1 = FXMLLoader.load(getClass().getResource("/resources/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(13);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(14);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(15);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(16);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(17);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(18);
System.out.println(e);
failures++;
}
System.out.println("There are this many failures: " + failures+"/18");
Scene scene = new Scene(root1, 300, 275);
primaryStage.setTitle("FXML Welcome");
primaryStage.setScene(scene);
primaryStage.show();
}
catch(Exception e)
{
System.out.println("XX"+e);
}
}
}
编辑:添加调用 resources/fxml/test.fxml 的代码也不成功。
我认为你需要更改这一行:
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));
我认为这不是您的 FXML 的正确路径。在我的项目中我们使用
resources
->fxml
text.fxml
我们这样加载它:
FXMLLoader loader = new FXMLLoader();
loader.load(Class.class.getClassLoader().getResource("fxml/text.fxml").openStream());
为了完全解决这个问题,我使用了 James_D 的 post 并检查了 IDEA 的构建到生产的工作原理。除非您告诉它,否则它不会保存文件。有几个编译器将带入生产环境的默认文件,例如 .属性 和 .jpg 文件...但不是以 .fxml 结尾的 JavaFX 文件。
我关注了这个:https://www.jetbrains.com/idea/help/resource-files.html
一旦我在接受文件的编译行末尾附加 ?*.fxml,我就能够获得一个构建来部署我的 fxml 文件。
谢谢大家。
我已经在 Whosebug 上阅读了所有关于此的问题,但我没有使用过有效的方法。我有一个基本的 fxml,它是在使用 Java 1.7.
时使用 JavaFX Scene Builder 1.1 构建的我只想加载文件...但所有内容似乎都指向一个空位置,这意味着它无法找到它。我不明白为什么。我有 18 try/catches 来显示 18 种不同的工作可能性,但它找不到它。这些示例是从一些 Whosebug 问题中提取的 'valid accepted answers'。我在这里错过了什么?一切都会编译,所以我不认为我缺少 SDK 或重要的东西。
日志打印 1 到 18,并在第 18 位出现 NullPointerException try/catch,表明 NullPointer... location is required.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class OptionsToggleMenu extends Application {
public void launchTheThing(String... args){ //runs this on the Main application's main method.
launch(args);
}
@Override
public void start(Stage primaryStage) {
int failures = 0;
try{
Parent root1 = new Parent() {
};
try{
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(1);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("AdventureLibrary/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(2);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(3);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(4);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(5);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(6);
failures++;
}
try{
root1 = FXMLLoader.load(getClass().getResource("/AdventureLibrary/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(7);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/AdventureLibrary/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(8);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("/bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(9);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(10);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(11);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(12);
failures++;
}
try{
root1 = FXMLLoader.load(getClass().getResource("/resources/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(13);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(14);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(15);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(16);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(17);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(18);
System.out.println(e);
failures++;
}
System.out.println("There are this many failures: " + failures+"/18");
Scene scene = new Scene(root1, 300, 275);
primaryStage.setTitle("FXML Welcome");
primaryStage.setScene(scene);
primaryStage.show();
}
catch(Exception e)
{
System.out.println("XX"+e);
}
}
}
编辑:添加调用 resources/fxml/test.fxml 的代码也不成功。
我认为你需要更改这一行:
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));
我认为这不是您的 FXML 的正确路径。在我的项目中我们使用
resources
->fxml
text.fxml
我们这样加载它:
FXMLLoader loader = new FXMLLoader();
loader.load(Class.class.getClassLoader().getResource("fxml/text.fxml").openStream());
为了完全解决这个问题,我使用了 James_D 的 post 并检查了 IDEA 的构建到生产的工作原理。除非您告诉它,否则它不会保存文件。有几个编译器将带入生产环境的默认文件,例如 .属性 和 .jpg 文件...但不是以 .fxml 结尾的 JavaFX 文件。
我关注了这个:https://www.jetbrains.com/idea/help/resource-files.html 一旦我在接受文件的编译行末尾附加 ?*.fxml,我就能够获得一个构建来部署我的 fxml 文件。
谢谢大家。