-1

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);
   }
}
anonymous
  • 13
  • 5

1 Answers1

0

All the Buttons in your GameButton array are null and you are adding them to your View. They will be used there eventually leading to a NPE.

Solution: Don't make the array static but instantiate your class instead.

import javafx.scene.layout.GridPane;

public class View extends GridPane {
    public View() {

        GameButton gb = new GameButton();

        for (int x = 0; x < gb.buttons.length; x++) {
            for (int y = 0; y < gb.buttons[x].length; y++) {
                this.add(gb.buttons[x][y], x, y);// Read out the array
            }
        }
    }
}

I'm not sure what you want to do here

import javafx.scene.control.Button;

public class GameButton extends Button {

    public Button[][] buttons = new Button[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 Button();   //Read in the array
            }
        }
    }
}
etaloof
  • 642
  • 9
  • 21
  • Ok now i use an instance of GameButton instead. But theres still a nullPointerException. I just dont get why the Array doesn't get filled in the GameButton-class. – anonymous Jan 01 '18 at 05:52
  • 1
    It works for me with the code I posted. Also the array is always filled. In your code it is "filled" automatically with null because the constructor is never called. In my example it is filled with a Button object. – etaloof Jan 01 '18 at 05:57