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