0

I have a gridpane 10x10 filled with buttons. I'm using this function to get button in specific position

private Node getNodeFromGridPane(GridPane gridPane, int col, int row)
{
    for (Node node : gridPane.getChildren()) {
        Integer colOf = GridPane.getColumnIndex(node);
        Integer rowOf = GridPane.getRowIndex(node);
        System.out.println("colof " + colOf + " " + rowOf);
        if (colOf == null || rowOf == null) {
            continue;
        } else {
            if (colOf == col && rowOf == row) {
                return node;
            }
        }
    }
    return null;
}

and assign it to arraylist

private Button [][] leftButtons = new Button[10][10];
private Button [][] rightButtons = new Button[10][10];

for(int i = 0; i < leftButtons.length; i++)
    {
        for (int j = 0; j <leftButtons[0].length; j++ )
        {
            leftButtons[i][j] = (Button) getNodeFromGridPane(leftGrid, i + 1, j + 1);
            rightButtons[i][j] = (Button) getNodeFromGridPane(rightGrid, i + 1, j + 1);
            System.out.println("rightButtons["+ i + "]["+ j + "] " + rightButtons[i][j]);
            rightButtons[i][j].setDisable(true); // <<<<<here I get NullPointerException
        }
    }

but rightButtons[0][9] gets null and I don't know why. There is button at that position in gridpane. It happend when i deleted first row and column from that gridpane. There is no buttons in that row and column.

Noone noticed that I add +1 to col and row when I call the function to compansate for extra row and column1

leftButtons[i][j] = (Button) getNodeFromGridPane(leftGrid, i + 1, j + 1);

So when I remove them no wonder that it returns null at the end of row. But when I changed it to

leftButtons[i][j] = (Button) getNodeFromGridPane(leftGrid, i, j);
rightButtons[i][j] = (Button) getNodeFromGridPane(rightGrid, i, j);

it returns null at 0,0

user3723497
  • 25
  • 1
  • 8
  • First of all, are you sure `leftButtons` and `rightButtons` are of the same sizes, and that every subarray of both is also of the same size? When you deleted first row and column, how could you get button on position [0][9]? It's deleted, isn't it (it's in first row, or first column, depending on what your array/subarray represents) – amorfis Sep 22 '17 at 17:07
  • Yes they are the same size private Button [][] leftButtons = new Button[10][10]; private Button [][] rightButtons = new Button[10][10]; Befor deletion gridpane was 11x11 – user3723497 Sep 22 '17 at 18:05
  • Does GridPane.getColumnIndex(node); returns null when column is 0? – user3723497 Sep 23 '17 at 13:16

0 Answers0