NullPointerException - JavaFX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20703434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:41:08  来源:igfitidea点击:

NullPointerException - JavaFX

javajavafx

提问by Shubham Gaikwad

I am getting NullPointerException in this code. I am using JavaFX 2.2 , NetBeans IDE 7.3.1, Windows 8, Java 1.7.0

我在这段代码中得到 NullPointerException。我使用的是 JavaFX 2.2、NetBeans IDE 7.3.1、Windows 8、Java 1.7.0

    public class SampleController implements Initializable {
    @FXML
    GridPane grid;
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        for(int i=0;i<10;i++){
            for(int j=0;j<4;j++){
                AnchorPane tile = new AnchorPane();
                tile.setMaxSize(225, 225);
                grid.add(tile, j, i);
            }
        }
    }
}

-JavaFX 2.2

-JavaFX 2.2

采纳答案by keyser

My guess is that your GridPaneisn't initialized. This would happen if you e.g. have a different ID for it in your FXML file.

我的猜测是您GridPane的未初始化。例如,如果您的 FXML 文件中有不同的 ID,就会发生这种情况。

It's a likely cause of the error, but I can't be sure without the full code.

这可能是导致错误的原因,但如果没有完整代码,我无法确定。

Also,

还,

you should learn how to debug a NullPointerException. It's often very simple. Here's something to get you started:

你应该学习如何调试一个NullPointerException. 这通常非常简单。这里有一些东西可以让你开始:

  1. Read the stack trace. Its first line looks something like this:
  1. 阅读堆栈跟踪。它的第一行看起来像这样:

Exception in thread "main" java.lang.NullPointerException. And if you don't see one or it doesn't give you any information, make sure you haven't caught the exception you're getting.

Exception in thread "main" java.lang.NullPointerException. 如果您没有看到一个或它没有为您提供任何信息,请确保您没有发现您遇到的异常。

  1. Find out what's null and where (the stack trace comes with line numbers.)
  2. Fix it (this usually means initializing something uninitialized, as in your case)
  1. 找出什么是 null 以及在哪里(堆栈跟踪带有行号。)
  2. 修复它(这通常意味着初始化一些未初始化的东西,就像你的情况一样)

And you might want to look up how to debug java programs in general (logging, stepping through your code at runtime etc). I've written a short (beginner's) blog poston the subject.

并且您可能想了解如何调试 Java 程序(日志记录、在运行时单步执行代码等)。我写了一篇关于这个主题的简短(初学者)博客文章

回答by Shandigutt

When you load an interface from a fxml file it takes bit of time to initialize all the components in that fxml. If you use those components before they get initialized then it gives a NullPointerException. What I personally do in my codes to get rid of this issue is I wait until the platform gets loaded and then start using those components. if you'd like to go for a solution like that follow the following code,

当您从 fxml 文件加载接口时,初始化该 fxml 中的所有组件需要一些时间。如果您在初始化之前使用这些组件,则会产生 NullPointerException。我个人在我的代码中为摆脱这个问题所做的是等待平台加载然后开始使用这些组件。如果您想寻求类似的解决方案,请遵循以下代码,

new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(new Runnable() {
                    public void run() {
                        //run your code here
                    }

                });

            }

        }, 500);

回答by Sijo Jose

 public class SampleController implements Initializable {
@FXML
GridPane grid = new GridPane();
@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    for(int i=0;i<10;i++){
        for(int j=0;j<4;j++){
            AnchorPane tile = new AnchorPane();
            tile.setMaxSize(225, 225);
            grid.add(tile, j, i);
        }
    }
}

} you have to initialize the GridPane

你必须初始化 GridPane

回答by jayaneetha

If the exception causes at the line grid.add(tile, j, i);, this means the GridPane gridis not initialized. You may have not added the fx:idattribute in the <GridPane ...>in your .fxml.

如果在 行引起异常grid.add(tile, j, i);,这意味着 GridPanegrid未初始化。您可能没有在 .fxml 中添加该fx:id属性<GridPane ...>

Your <GridPane>should be similar to <GridPane fx:id="grid" ... >

<GridPane>应该类似于<GridPane fx:id="grid" ... >

回答by Marilynn

Initialize the "GridPane grid;" to something either from the scent builder by setting the fx:id to "grid" or through the code here.

初始化“GridPane 网格”;通过将 fx:id 设置为“grid”或通过此处的代码从气味生成器中获取某些内容。

回答by captainchhala

Several reason can cause this problem. 1. GridPane has @FXML tag which means it should be initialized with fxml loader, one possible reason is that you have not given the id of grid to GridPane in scenebuilder. 2. Another reason could be to specify the name of the FXML controller incorrectly. So you have to specify correct name of the package (if controller is not in default package) followed by (.) and controller name.

有几个原因会导致这个问题。1. GridPane 有@FXML 标签,这意味着它应该用fxml loader 初始化,一个可能的原因是你没有在scenebuilder 中给GridPane 提供grid 的id。2. 另一个原因可能是错误地指定了 FXML 控制器的名称。因此,您必须指定正确的包名称(如果控制器不在默认包中),后跟 (.) 和控制器名称。