0

I set up the gridpane like so:

for (int row = 0; row < mainBoard.getRows(); row++){
    for (int column = 0; column < mainBoard.getColumns(); column++) {
        mainBoard.setStatusArray(row, column, 0);
        Pane pane = new Pane();
        GridPane.setRowIndex(pane, row);
        GridPane.setColumnIndex(pane, column);
        gridpane.getChildren().add(new Pane());
        System.out.println(GridPane.getRowIndex(pane));
    }
}

And try to access it like so:

ObservableList<Node> childrens = gridpane.getChildren();
    for (Node node : childrens) {
        if (node instanceof Pane
            && GridPane.getRowIndex(node) == 1        <------------- line of error
            && getColumnIndex(node) == 1) {
                // Do stuff
    }
}

It seems that GridPane.getRowIndex(node) Always returns null, thus causing a NullPointerException and I cant figure out for the life of me why.

Rudra Mutalik
  • 372
  • 4
  • 17
  • The only time I get `null` returned is if the grid pane contains other child nodes that have not had their column and row indexes set (e.g. if you have grid lines visible). Your code should work fine if you just provide a null check. – James_D Mar 10 '20 at 16:11

1 Answers1

0

A solution found here may be helpful: GridPane.getColumnIndex() returning null

The user was having the same problem and by changing how they added the elements to the gridpane, it worked.

The answer provided by user Matt is:

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        GridPane grid = new GridPane();
        grid.setGridLinesVisible(true);

        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                grid.add(new Button("foo"),i,j);
                //                Button myBtn = new Button("foo");
                //                GridPane.setColumnIndex(myBtn, i);
                //                GridPane.setRowIndex(myBtn, j);
                //                grid.getChildren().add(myBtn);
            }
        }


        Scene scene = new Scene(grid);

        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();

        String[] colors = {"-fx-background-color: red;", "-fx-background-color: blue;", "-fx-background-color: white;"};
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                Node node = getNodeFromGridPane(grid,i,j);
                if(node instanceof Button)
                    node.setStyle(colors[(int) (Math.random() * 3)]);
            }
        }
    }

    private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
        for (Node node : gridPane.getChildren())
            if (GridPane.getColumnIndex(node) != null
                    && GridPane.getColumnIndex(node) != null
                    && GridPane.getRowIndex(node) == row
                    && GridPane.getColumnIndex(node) == col)
                return node;
        return null;
    }

    public static void main(String[] args) { launch(args); }
}
MrsNickalo
  • 207
  • 1
  • 6
  • 2
    Is there a reason why you repeated the answer rather than just posting a link to it? Do you not believe in the OOP principle DRY? – Abra Mar 10 '20 at 15:52
  • 1
    The appropriate response is to click the flag link and mark the question as a duplicate of the existing answer. – MarsAtomic Mar 10 '20 at 16:37
  • Sorry, I'm still new to Stack Overflow...when you mark it as duplicate, can you provide the link? – MrsNickalo Mar 10 '20 at 20:05