1

I currently have a problem with creating objects using two-dimensional arrays in Java.

I'ld like to display bricks on a playing board (like in the classic "BreakOut games), therefore I created a class "Stone" for the stone-objects, the constructor of the class is:

public Stone (Position pos, int value){
    this.pos = pos;
    this.type = type;
}

I also created a 2D-array (int) called "stoneTypes" for the pattern of the Stones, in which I saved a matrix from a JSON file.

I ld' now like to create Stone-objects in my class "Level" by using the values of the stoneTypes-matrix, which currently looks like this (I included an if-condition, so Stone-objects are only created for stoneTypes value >= 1):

private Stone[][] stones = new Stone[25][20];

private int[][] stoneTypes;

JSONReader reader = new JSONReader("res/Level" + levelnr +".json");
stoneTypes = reader.getStones2DArray();

for (int w = 0; w < stoneTypes.length; w++) {
       for (int h = 0; h < stoneTypes[w].length; h++) {

          if (stoneTypes [w][h] >= 1) {
              Position pos = new Position(width * w, height * h);
              this.stones[w][h] = new Stone(pos, stoneTypes[w][h]);
          }


       }
}

I also included a get-Method for the Stone-array, so I could use it to draw the Stones in my "Field" class:

public Stone[][] getStones(){
    return this.stones;
}

The method for drawing the Stones in my "Field" class currently looks like this:

private void drawStones(Graphics2D g2) {
stones = view.getGame().getLevel().getStones();
  for (int i = 0; i < stones.length; i++) {
    for (int j = 0; j < stones[i].length; j++) {
    ***     int x_position = (int) stones[i][j].getPosition().getX();           ***
            int y_position = (int) stones[i][j].getPosition().getY();
            g2.fillRoundRect(x_position, y_position, 
                    (int) ((double)Constants.SCREEN_WIDTH/Constants.SQUARES_X)-2, 
                    (int) ((double)Constants.SCREEN_HEIGHT/Constants.SQUARES_Y)-2 ,1,1);
            System.out.println(x_position);
    }
  }
}

Eclipse doesn't show any syntax errors but I do receive a NullPointerException at the spot I marked with the ***, as soon as I start the programm. I am not sure if my get-Method isn't implemented correctly or if the process of creating new Stone-objects is simply wrong. I tried hundreds of things but couldn't find a solution, I hope you guys can help me.

Thx in advance, Scoopa!

GhostCat
  • 127,190
  • 21
  • 146
  • 218
Scoopa
  • 31
  • 2

1 Answers1

1

Here:

if (stoneTypes [w][h] >= 1) {
  Position pos = new Position(width * w, height * h);
  this.stones[w][h] = new Stone(pos, stoneTypes[w][h]);
}

You are only creating new stones when that condition is met. All other fields will stay with their default initial value. And that would be: null.

When you do that, you can't just go in (unconditionally) and do:

int x_position = (int) stones[i][j].getPosition().getX()

You would need a

if (stones[i][j] != null) 

to guard such accesses!

GhostCat
  • 127,190
  • 21
  • 146
  • 218