joejoethemonkey
joejoethemonkey

Reputation: 63

2d Array with Rectangle() grid

I am trying to make a grid, using a 2d array list, I want to make it with Rectangles so I can use .intersects with it, I also need it to be 95 x 95 and 95 apart, this is what I have so far but its not working because of an error.

    public static Rectangle[][] walls;
    public static void walls() {
    int wallsY = 0, wallsX =0;
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            //shapeList.add(new Rectangle(wallsX, wallsY, 95, 95));
            walls[i][j] = new Rectangle(wallsX,wallsY,95,95);
            wallsY += 95;
            wallsX += 95;

        }
    }

then i use:

    for (int i = 0; i < walls.length; i++) {
        for(int j =0; j < walls.length; j++){
            if (intersectsBox(playerRectangle(), walls[i][j])) {
                isInsideWalls = true;
            }   
        }
     }

to check if they are intersecting. But I keep getting an error which is right here:

`Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at bombermangame.Game.walls(Game.java:165) at bombermangame.Game.(Game.java:62) at bombermangame.Menu.actionPerformed(Menu.java:98) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source

Upvotes: 1

Views: 1224

Answers (1)

Dan12-16
Dan12-16

Reputation: 351

You never initialized walls. Prior to your for loop for (int i = 0; i < 7; i++), add:

walls = new Rectangle[7][7];

I put 7 and 7 in there because that is what it appears the dimensions will be.

Upvotes: 2

Related Questions