I got an exercise on implementing a javafx-application of Minesweeper to do. I took an excerpt of the code in order to isolate the problem. In the code i read-in an Array of objects of type javafx.scene.control.Button. In another class i read out this array. Like in every javafx Application the whole is brought together in an Application-class. Compiling works. I get a nullPointerException when i run the Programm. That is my problem here. Probably the Array doesn't contain any objects. By the way i use separated classes because the exercise consists in the model-view-controller Designpattern.
import javafx.scene.control.Button;
public class GameButton extends Button {
public static GameButton[][] buttons = new GameButton[3][4];
public GameButton() {
for (int x = 0; x < buttons.length; x++) {
for (int y = 0; y < buttons[x].length; y++) {
buttons[x][y] = new GameButton(); //Read in the array
}
}
}
}
import javafx.scene.layout.GridPane;
public class View extends GridPane {
public View() {
for (int x = 0; x<GameButton.buttons.length; x++) {
for (int y = 0; y<GameButton.buttons[x].length; y++) {
this.add(GameButton.buttons[x][y], x, y);// Read out the array
}
}
}
}
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class Main extends Application {
public void start(Stage stage) {
View view = new View();
Scene scene = new Scene(view);
stage.setTitle("Minesweeper");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}