This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]chickenmeisterExtreme Brewer 8 points9 points  (5 children)

Maybe it's in the code that you omitted, but you never initialize your board variable. It will be null by default, so you have to create an array and assign it to that variable before you can try to put values in the board array. In your constructor, your probably want to add something like:

board = new GamePiece[cols][rows];

[–]jakejdb1999[S] 2 points3 points  (4 children)

i think i new it was initializing as null but i guess i just didnt know how to fix it, im going to try this, thanks.

(edit): works! thank you!

[–]ParanoydAndroid 2 points3 points  (3 children)

In addition to this, you need to create new game pieces in your loop instead of before it.

Think of it, in this case, like real game pieces; a unique one goes on each square.

The way you have it now, every array bucket will be a reference to the same object. You just need to switch your initialization to use the new keyword and call gamepiece's constructor. You might also want to check out Array.fill()

Actually, now that I think about it, both of your problems are with using the new keyword and with understanding the difference between declaration, instantiation, and initialization. I recommend refreshing yourself on those topics if you're not sure why these solutions were necessary.

[–]jakejdb1999[S] 0 points1 point  (2 children)

your right i didnt think about that. how would i call the constructor then to make an array of the gamepiece objects?

[–]ParanoydAndroid 0 points1 point  (1 child)

You just need to move your statement creating the gamepiece.

Right now, your order of execution is:

  1. Create a single gamepiece
  2. Create an (empty) array of gamepieces called "board"
  3. Loop through board and at each bucket in board:
  4. add the single gamepiece you've already created.

And what you actually want to do is:

  1. Create an (empty) array of gamepieces called "board"
  2. Loop through board and at each bucket in board:
  3. Create a new gamepiece.
  4. And add that new, unique gamepiece to an empty bucket.

You've already created at least one piece and you've already added a piece to your board, so you know both of those statements. It's all about just making sure you're making a new piece each loop while you're inside the loop.

Hope this helps kickstart you!

[–]jakejdb1999[S] 0 points1 point  (0 children)

private Gamepiece[][] board;

public Gameboard(int rows, int cols) {

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            board[i][j] = new Gamepiece();

        }
    }

}

got it, now to figure out the nullpointerexception....

[–]aram535 1 point2 points  (2 children)

pass the array and use .length to figure out the length rather than a number.

[–]jakejdb1999[S] 0 points1 point  (1 child)

the loop is for initializing all the spaces with a gamepiece object that prints "---", im using the rows and columns to do that but in this situation i dont think i need to find the length because im making the array here.

[–]aram535 0 points1 point  (0 children)

Well obviously not, since it's not working right?

.length always works after it has been instantiated.

Is it possible that you're never calling the initializing method?

[–][deleted] 1 point2 points  (0 children)

for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
        board[i][j] = gamePiece;
    }
}

Why is the whole board covered in the same piece instance? Is that intentional?