0

I am working on a simple LiteBrite game using javaFX. Using a color picker, the user chooses a color and clocks on the GridPane which is a collection of Rectangles. Each click changes the color of that Rectangle to the one chosen by the user. However, if the color of the Rectangle has already been changed, I am expected to change the color back to the original (black). I am having trouble accessing the Node and casting it to Rectangle (in order to check the color). This is the method I am using to get a Node:

private Node getNode(GridPane grid, int column, int row){
      Node result = null; 
      for (Node node : grid.getChildren()) {
           if (GridPane.getColumnIndex(node) == column 
                   && GridPane.getRowIndex(node) == row) {
                result = node;
            }
      }
      return result;
}

In the start method I call the the above method and save the Node:

      Node node = getNode(grid,i,j);

At this point, if I try to print out node I have no problem. However, after I cast the Node into Rectangle and try to check its color I get a NULLPointerException:

       Rectangle rec = (Rectangle) node;
       System.out.println(rec.getFill());

What am I doing wrong? Any help is greatly appreciated. Thank you in advance.

Steven
  • 604
  • 6
  • 11

0 Answers0