0

I am writing Java code for a Monopoly game and I am getting a NullPointerException on the line with the SetPlayerName(tempName, numPlayersCounter); method call. This method sets the name of a Player in a Player array. When the program is run, the console says that the value of numPlayersCounter is 1, the length of the Player array playersArr is 3.

        sc2.ok1Button.addActionListener(new ActionListener() //middle ok button
    {
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Name ok button pressed");
            System.out.println("Name is:" + sc2.nameField.getText());
            String tempName = sc2.nameField.getText();
            System.out.println("numPlayersCounter value: " + numPlayersCounter);

            SetPlayerName(tempName, numPlayersCounter);
            System.out.println("Player " + numPlayersCounter + "name: " +playersArr[numPlayersCounter].GetName());
            sc2.EnableName(false);
            sc2.EnablePieces(true);


        }
    });

The method SetPlayerName is defined as:

    public static void SetPlayerName(String n, int id)
    {   playersArr[id].SetName(n);    }

and playersArr is declared before the constructor and main as:

    protected static Player [] playersArr; //player 0 is the bank   

In Player.java, the name is set as an empty string in the constructor and the SetName method is defined as:

    public void SetName(String n)
    {   name = n;   }

I have tried to set the name directly from the actionPerformed method and incrementing a different Player variable. The problem seems to be with accessing the correct array element, but it should be accessing element 1 of an array of length 3.

What do I need to do to fix this? Any help would be greatly appreciated.

  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – jdphenix Apr 20 '15 at 02:46
  • 6
    You never initialize your array `playersArr` (so it is `null`). – Elliott Frisch Apr 20 '15 at 02:47
  • _"What do I need to do to fix this?"_ change to something like `protected static Player [] playersArr = new Player[10];` but I would recommend you to use `ArrayList` instead of `array` – DnR Apr 20 '15 at 02:51
  • I initialize it in my constructor and then point playersArr to a new array in a previous actionPerformed method. Sorry I didn't add that before. I will update my question to include this. – Laura Ann Truax Apr 20 '15 at 02:52
  • @jdphenix, I was looking at that post earlier and I was not able to use it to my advantage – Laura Ann Truax Apr 20 '15 at 02:56
  • 2
    then its not `playersArr` null, but `playersArr[id]` – Baby Apr 20 '15 at 02:59
  • @DnR initializing when I declare the array is not working either. I will look into the ArrayList. – Laura Ann Truax Apr 20 '15 at 02:59
  • @Baby, the value being passed into id is 1 and the length of the array is 3 (according to the console when the program is run), so 1 should be a valid location – Laura Ann Truax Apr 20 '15 at 03:02
  • try printing the array content at the same location you print the array length: `System.out.print(Arrays.toString(playersArr));` what is the output? – Baby Apr 20 '15 at 03:05
  • I bet your output is `[null, null, null]` . isn't it? – Baby Apr 20 '15 at 03:15
  • @Baby you were right! The output is [null, null, null] . Now I need to find out why playersArr = new Player[numPlayers+1]; dosen't initialize 3 new Player objects. – Laura Ann Truax Apr 20 '15 at 03:19
  • I have no default constructor... – Laura Ann Truax Apr 20 '15 at 03:20
  • have you tried just `new Player()`?. you don't have to define a default constructor. default constructor is exist by default – Baby Apr 20 '15 at 03:27
  • 1
    I changed the constructor to a default constructor (parameters were not necessary) and Gary posted the answer I was looking for. Thank you too, Baby. You helped me a lot! – Laura Ann Truax Apr 20 '15 at 03:31

3 Answers3

0

From what is given, you have not initialized your playersArr array. On the line:

protected static Player [] playersArr;

or anytime because you call the SetPlayerName() method you need to initialize it. For example:

protected static Player [] playersArr = new Player[]{/*Stuff in array*/};
Forseth11
  • 1,418
  • 1
  • 12
  • 21
0

Reason:

You have an empty array list, i.e., playerArr[0] = null; and your SetPlayerName method references playerArr[id] which contains a null pointer.

Fix:

You need to add Player objects to your playerArr before trying to use them, try this:

playerArr[numPlayersCounter] = new Player();
SetPlayerName(tempName, numPlayersCounter);

You should also initiate the playerArr too, with 3 elements...:

protected static Player[] playerArr = new Player[3];
Gready
  • 1,134
  • 8
  • 15
  • 1
    That is exactly what needed to happen (with the exception of playerArr being playersArr , with an s). Thank you very much @gary-read – Laura Ann Truax Apr 20 '15 at 03:28
0

Have you initialized the Player?

playersArr[numPlayersCounter] = new Player();      

and then

SetPlayerName(tempName, numPlayersCounter);      
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
Suspended
  • 1,184
  • 2
  • 12
  • 28