JavaFX FXMLLoader上的NullPointer位置 [英] NullPointer location on JavaFX FXMLLoader

查看:316
本文介绍了JavaFX FXMLLoader上的NullPointer位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在stackoverflow上阅读了有关这方面的所有问题,并且我没有使用过有用的东西。我有一个基本的fxml,它是在使用Java 1.7时使用JavaFX Scene Builder 1.1构建的。



我只是想加载文件...但是一切似乎都指向一个空位置意味着它无法找到它。我不明白为什么。我有18个尝试/捕获,以显示18种不同的工作可能性,但它无法找到它。这些示例从一些stackoverflow问题中提取为有效接受的答案。我在这里错过了什么?所有东西都编译,所以我不认为我错过了一个SDK或者一些主要的东西。



日志打印1到18,在第18次try / catch上显示NullPointerException NullPointer ...位置是必需的。



  import javafx.application.Application; 
import javafx.fxml.FXMLLoader;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.net.URL;


公共类OptionsToggleMenu extends Application {

public void launchTheThing(String ... args){//在主应用程序的main方法上运行它。
launch(args);
}

@Override
public void start(Stage primaryStage){
int failures = 0;

try {
父root1 = new Parent(){
};
try {
root1 = FXMLLoader.load(getClass()。getResource(AdventureLibrary / test.fxml));
System.out.println(null == root1);
}
catch(例外e){
System.out.println(1);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(AdventureLibrary / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(2);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getResource(bin / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(3);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(bin / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(4);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getResource(test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(5);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(6);
失败++;
}
try {
root1 = FXMLLoader.load(getClass()。getResource(/ AdventureLibrary / test.fxml));
System.out.println(null == root1);
}
catch(例外e){
System.out.println(7);
失败++;
}

try {
Parent root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(/ AdventureLibrary / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(8);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getResource(/ bin / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(9);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(/ bin / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(10);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getResource(/ test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(11);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(/ test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(12);
失败++;
}
try {
root1 = FXMLLoader.load(getClass()。getResource(/ resources / test.fxml));
System.out.println(null == root1);
}
catch(例外e){
System.out.println(13);
失败++;
}

try {
Parent root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(/ resources / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(14);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getResource(resources / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(15);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(resources / test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(16);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getResource(test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(17);
失败++;
}

try {
父root2 = FXMLLoader.load(getClass()。getClassLoader()。getResource(/ test.fxml));
System.out.println(null == root2);
}
catch(例外e){
System.out.println(18);
System.out.println(e);
失败++;
}


System.out.println(有很多失败:+失败+/ 18);

场景场景=新场景(root1,300,275);

primaryStage.setTitle(FXML欢迎);
primaryStage.setScene(scene);
primaryStage.show();
}
catch(例外e)
{
System.out.println(XX+ e);
}
}


}



<编辑:添加调用resources / fxml / test.fxml的代码也不成功。

解决方案

为了完全解决这个问题,我拿了James_D的发布并检查IDEA如何构建生产工作。除非您告诉它,否则它不会保存文件。编译器将为生产带来几个默认文件,如.property和.jpg文件...但不是像.fxml那样的JavaFX文件。



我跟着这: https://www.jetbrains.com/idea/help/resource-files .html
我能够获得一个构建来部署我的fxml文件一旦我出现了?*。fxml在编译行的末尾接受文件。



谢谢你们。


I've read through all of the issues about this on stackoverflow, and I haven't used something that worked. I have a basic fxml that is built using the JavaFX Scene Builder 1.1 while using Java 1.7.

I just want to load the file... but everything seems to point to a null location meaning that it cant find it. I don't understand why. I have 18 try/catches to show 18 different possibilities for it to work under, but it cant find it. These examples are pulled from some of the stackoverflow questions as 'valid accepted answers'. What am I missing something here? Everything compiles, so I dont think I'm missing a SDK or something major.

The log prints 1 to 18, with a NullPointerException on the 18th try/catch that says NullPointer... location is required.

View post on imgur.com

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);
    }
}


}

Edit: Adding code that calls on resources/fxml/test.fxml is unsuccessful as well.

解决方案

To fully solve this issue, I took James_D's post and checked how IDEA's build to production works. It doesn't save files unless you tell it to. There are several default files that the compiler will bring to production like .property and .jpg files... but not JavaFX files like that end in .fxml.

I followed this: https://www.jetbrains.com/idea/help/resource-files.html I was able to get a build to deploy my fxml file once I appened ?*.fxml at the end of the compile line for accepted files.

Thanks guys.

这篇关于JavaFX FXMLLoader上的NullPointer位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆