0

Here is a method called placeShips() that I am calling via another method (which is called by a ButtonListener). But when all are called, I get a NullPointerException on the deepest nested line - System.out.println(ships[i]);. The array has been declared and initialized above this code in the constructor. It is set to be equal to a constant integer which equals 3. I've put a simple string in that printout and it works. But whenever the array gets involved, it becomes messy. What is going wrong?

NUM_SHIPS, NC_EMPTY, NC_SHIP, and all the labels/buttons have been made as well.

private Ships ships[];

*-----Constructor begins here-----*
Ships[] ships = new Ships[NUM_SHIPS];
*-----Constructor ends here-----*

ships[0] = new Ships("Aircraft Carrier", 5, false);
ships[1] = new Ships("Battleship", 4, false);
ships[2] = new Ships("Cruiser", 3, false);

public void placeShips()
{
    statusLabel.setText("Press [Play]");
    int shipsPlaced = 0;

    do
    {
        int randomRow = (int)(Math.random()*ROWS);
        int randomCol = (int)(Math.random()*COLS);

        if (gameBoard[randomRow][randomCol] == NC_EMPTY)
        {
            gameBoard[randomRow][randomCol] = NC_SHIP;
            shipsPlaced = shipsPlaced + 1;

            for (int i = 0; i < NUM_SHIPS; i++)
            {
                System.out.println(ships[i]);
            }
        }
    }while (shipsPlaced < NUM_SHIPS);
}
9
  • Is NUM_SHIPS equal to ships.length ? Commented Apr 11, 2011 at 1:21
  • is placeShips() and ships[] array, members of the same class ? Have you tried putting a breakpoint at println statement and see if ships array has ship objects or is the array empty ? Commented Apr 11, 2011 at 1:22
  • Do you have a class level array variable called "ships"? Are you sure you're assigning to it before you try to use it in PlaceShips()? Also, its better to construct your for loop as for (int i = 0; i < ships.Length; i++){...} Commented Apr 11, 2011 at 1:22
  • NUM_SHIPS is equal to ships.length. Commented Apr 11, 2011 at 1:25
  • About the class, I have a separate Ships.java file that holds the class and constructor for Ships. Commented Apr 11, 2011 at 1:26

2 Answers 2

1

You have a class level array variable : private Ships ships[];, yet you define in your constructor Ships[] ships = new Ships[NUM_SHIPS];. Are you ever assigning to the class level variable? you could try

/* Class Level */
private Ships _ships[];

/* In constructor */
_ships = new Ships[NUM_SHIPS];

/* In PlaceShips() */
for (int i = 0; i < _ships.Length; i++)
{
    if(_ships[i] != null)
    {
        System.out.println(_ships[i].toString());
    }
}

If that doesn't work then debug the code to find which object is actually throwing the exception

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like your constructor is only initializing a local variable named ships. You say you have:

-----Constructor begins here-----*
Ships[] ships = new Ships[NUM_SHIPS];
*-----Constructor ends here-----*`

But it seems like you really want

ships = new Ships[NUM_SHIPS];

1 Comment

This also helped, turns out I didn't need the Ships[] in the beginning. It no longer spits out the error! Thanks everyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.