0

I am getting NullPointerException in this code. I am using 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

Shubham Gaikwad
  • 103
  • 2
  • 2
  • 9

6 Answers6

11

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

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:

  1. Read the stack trace. Its first line looks something like this:

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.

  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)

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 post on the subject.

Community
  • 1
  • 1
keyser
  • 18,829
  • 16
  • 59
  • 101
  • 2
    Actually, the GridPane is annoted with @FXML which means it's created by the FXMLLoader. The way to resolve this would be to make sure the FXML file has a GridPane component with a fx:id set to "grid". There's also the possibility that the Grid does not have the sufficient amount of rows and columns but I'm not sure if that would throw a NPE – Alexandre Dec 21 '13 at 20:04
  • @PeekaySwitch +1 Good point, I updated my question accordingly. To OP: Step one is still to debug your program. If your `GridPane` is null you might want to look into FXML specific issues such as the above mentioned one regarding ID's. – keyser Dec 22 '13 at 00:54
  • 2
    I can't believe this! It was just a spelling mistake. the fx:id was gird and in Controller I declared it as grid. But what is NPE, stack trace? – Shubham Gaikwad Dec 22 '13 at 09:01
  • NPE = Null Pointer Exception. – ManoDestra Feb 24 '16 at 16:35
0
 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

Sijo Jose
  • 363
  • 1
  • 3
  • 10
0

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

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

jayaneetha
  • 75
  • 1
  • 1
  • 8
0

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

Marilynn
  • 71
  • 5
0

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.

captainchhala
  • 831
  • 1
  • 7
  • 14
-2

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,

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

                });

            }

        }, 500);
Shandigutt
  • 11
  • 3