while creating an application using JavaFX i faced a little problem - elements in my array are somehow not updating with changes which are made on these elements. On the start I want to point - dont pay attention on structure and model of my app - I already know It is not good and I changed it but i still dont understand why my problem existed. This how initialization of my array look like:
public class GameBoardAI implements IGameModel{
Random rand = new Random();
int currentPlayer = 1;
TicTacViewController tacController = new TicTacViewController();
Button[] buttonss = new Button[]{ tacController.btn1, tacController.btn2, tacController.btn3, tacController.btn4,
tacController.btn5, tacController.btn6, tacController.btn7, tacController.btn8, tacController.btn9};
The problem is, that when I am creating the array of buttons, buttons are not connected to the view yet, so they are still nulls. And when I am trying to invoke some methods on my buttons inside I encounter a problem:
public void switchPlayer() {
if(currentPlayer == 1)
{
currentPlayer=2;
buttonss[rand.nextInt(9)].fire();
}
if(currentPlayer == 2)
currentPlayer = 1;
}
You can see here, that I am trying to get some random button, from array of buttons i created in instance variable. Here is the part of code, when buttons are located inside TicTacViewController:
public class TicTacViewController implements Initializable
{
@FXML
private Label lblPlayer;
@FXML
private Button btnNewGame;
@FXML
private GridPane gridPane;
private static final String TXT_PLAYER = "Player: ";
private IGameModel game = new GameBoard();
@FXML
public Button btn1;
@FXML
public Button btn2;
@FXML
public Button btn3;
@FXML
public Button btn4;
@FXML
public Button btn5;
@FXML
public Button btn6;
@FXML
public Button btn7;
@FXML
public Button btn8;
@FXML
public Button btn9;
As i understood, the problem is, that when I am creating the array as instance variable, mu buttons are still null - they are not connected to the view yet. But here happens some strange thing: When I am putting array initialization inside a switchPlayer method instead of doing this as instance variable - everything is working correctly. So it seems like when I am creating array while invoking a method buttons are already connected to the view and there is no problems. And it ruins my knowledge about reference variables - why it is not working when we are creating array as instance variable? Because i thought that even if we have a reference variables inside a array - when we will change this reference variables, they will be also changed in our array. To be more specific - even if when we are initalizing an array and buttons are not connected to the view yet, they are connecting afterwards - so when we are invoking a switchPlayer method butttons should be already connected to the view - but compiler is telling me that they are null. Can someone explain me what is the problem here? Why while invoking a method buttons are still null, as they were in array creation even if they were connected to the view afterwards?