1

I keep getting this error:

Exception in thread "main" java.lang.NullPointerException at BattleshipCMDGame.GenerateShips(BattleshipCMDGame.java:33) at BattleshipCMDGame.main(BattleshipCMDGame.java:7)

All I want to do is return the newly created class type array in my method into an empty array created in the main method. Here is my code:

import java.util.*;

public class BattleshipCMDGame
{
public static void main(String[] args)
{
    Ship[] ship = GenerateShips(3);
    Scanner in = new Scanner(System.in);

    for (int i = 0; i < ship.length; i++)
    {
        System.out.println(ship[i].GetName() + " : Location - " + ship[i].GetLocation());
    }
}

public static Ship[] GenerateShips(int numShips)
{
    Ship[] ship = new Ship[numShips];
    Random rand = new Random();
    int randLoc;
    String prevRands = "";
    String randToString = "";

    for (int i = 0; i < ship.length; i++)
    {
        randLoc = 1 + rand.nextInt(7);
        randToString = Integer.toString(randLoc);

        for (int z = 0; z < ship.length; z++)
        {
            prevRands = "";

            if (ship[z].GetLocation() != 0)
            {
                prevRands += Integer.toString(ship[z].GetLocation());
            }
        }

        while (prevRands.contains(randToString))
        {
            randLoc = 1 + rand.nextInt(7);
            randToString = Integer.toString(randLoc);
        }

        ship[i] = new Ship("Ship no. " + (Integer.toString(i)), randLoc);
    }

    return ship;
}
}
2
  • As noted with the migration, implementation questions are best dealt with on SO rather than P.SE. Questions of working with null pattern, or the best choice of passing back null vs an empty ArrayList, or asking about points of the The Billion Dollar Mistake would be more appropriate for P.SE (though look, there are other questions on this topic). Commented Aug 20, 2013 at 18:13
  • As an aside, particular to this code, please look at StringBuilder rather than doing prevRands += Integer.toString(...); You are likely creating and discarding lots of unnecessary strings. Commented Aug 20, 2013 at 18:15

3 Answers 3

3
if (ship[z].GetLocation() != 0)

ship[z] is empty (null), so that's why you get the error. You need to fill yout ship array first.

The important thing is: ship array stores references not objects, so that's why you need to fill it first. So

Ship[] ship = new Ship[10]

stores 10 Ship references (which starts as null), the actual Ship objects you need to assign by yourself.

3

You have created the array in this line:

Ship[] ship = new Ship[numShips];

But all elements are null, so a NullPointerException results on this line:

if (ship[z].GetLocation() != 0)

You need to assign Ship objects to locations in your array, something like this:

ship[z] = new Ship();
1
  • Yeah, I actually solved the mistake on my own and you are right. Instead I just created a int count variable and increment it everytime I actually assign a ship var to a new ship object. I replaced a number in a for loop with count, that way it knows how many ships objects actually have reference from the array. Commented Aug 20, 2013 at 18:17
2

You should initialize every element of the array:

ship[z] = new Ship();

Btw, you should use method names that starts with lower case, that's the standard

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.