首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointer在JavaFX FXMLLoader的位置

NullPointer在JavaFX FXMLLoader的位置
EN

Stack Overflow用户
提问于 2016-02-25 14:43:02
回答 2查看 4.1K关注 0票数 0

我已经阅读了关于堆栈溢出的所有问题,而且我没有使用一些有用的东西。我有一个基本的fxml,它是使用JavaFX场景生成器1.1生成的,同时使用Java1.7。

我只想载入文件..。但一切似乎指向一个空位置,这意味着它找不到它。我不明白为什么。我有18次尝试/捕捉来展示它在18种不同的可能性下工作,但它找不到。这些例子是从一些堆叠溢出的问题中抽取出来的,它们是“有效的、可接受的答案”。我漏掉了什么东西?所有的东西都会编译,所以我不认为我缺少一个SDK或一些重要的东西。

日志打印1到18,第18次尝试/捕捉时有一个NullPointerException,上面写着NullPointer.需要地点。

在imgur.com上查看帖子

代码语言:javascript
复制
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);
    }
}


}

编辑:添加调用资源/fxml/test.fxml的代码也是不成功的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-25 15:49:32

为了完全解决这个问题,我接受了James_D的帖子,并检查了IDEA的构建是如何工作的。除非你告诉它,否则它不会保存文件。编译器将为生产带来几个默认文件,如.property和.jpg文件.但这样的JavaFX文件在.fxml中没有结束。

我遵循了以下步骤:在编译行的末尾为接受的文件追加了https://www.jetbrains.com/idea/help/resource-files.html之后,我就能够获得一个构建来部署我的fxml文件。

谢谢你们。

票数 2
EN

Stack Overflow用户

发布于 2016-02-25 14:58:08

我认为你需要改变这句话:

代码语言:javascript
复制
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));

我不认为这是通往FXML的正确路径。在我的项目中我们使用

代码语言:javascript
复制
resources
    ->fxml
        text.fxml

我们像这样装载它:

代码语言:javascript
复制
FXMLLoader loader = new FXMLLoader();
loader.load(Class.class.getClassLoader().getResource("fxml/text.fxml").openStream());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35630415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档