Reputation: 123
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
Upvotes: 0
Views: 17560
Reputation: 825
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.
Upvotes: 0
Reputation: 71
Initialize the "GridPane grid;" to something either from the scent builder by setting the fx:id to "grid" or through the code here.
Upvotes: 0
Reputation: 77
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" ... >
Upvotes: 0
Reputation: 363
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
Upvotes: 0
Reputation: 11
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);
Upvotes: -2
Reputation: 19189
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:
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.
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.
Upvotes: 11