tags:

views:

117

answers:

4

I get a null pointer exception when a call a method on a custom class. I do not see why my object p of class Player is null. (Player is a class of Players of my game) play is the method. This class should create n number of p objects of the Player class and run the play method on them.

import java.util.*;
public class PsychicGameMore
{
    private ArrayList <Player> players;
    private int orginalNumber;
    private Player p; 

    public PsychicGameMore()
    {



        int n;
        Scanner s;
        s = new Scanner(System.in);
       System.out.println("How many players will there be?, Please enter a number");
       n = s.nextInt();
       players = new ArrayList<Player>(n);

        //for loop to create n number of Players 
        for(int i = 0; i < n; i++)
        {
           Player p;
            p = new Player();


            players.add(p);
        }

       orginalNumber =0;


    }

     public void play()
      {
        Random myRandom;
        myRandom = new Random();

    do 
    {
    orginalNumber = myRandom.nextInt(6)+1;
    System.out.println("The computer has choosen the number " + orginalNumber);
    p.play();

        if(orginalNumber == p.getGuessedNumber())
        {
        System.out.println(p.getName() + " has won!!!!!!!!!!!!!!!!");
        }

      } while((orginalNumber != p.getGuessedNumber()));


     }
}

Thanks for any help.

+4  A: 

You assign quite a few Players to your arraylist players, but you never assign any to p. Note that in the loop:

   for(int i = 0; i < n; i++)
    {
       Player p;
        p = new Player();

        players.add(p);
    }

The local p hides the one in the object. It is not clear what value you want the object-level p to have, so I have no idea how to correct your code.

You should also really work on keeping your block indentation consistent. It is hard to follow your code as is. If you're using eclipse, fix it up by highlighting the whole mess and hitting ctrl-i.

Donnie
A: 

p at

p.play();

is null. You never create it. The only instances of Player being initialized are the ones your adding to your ArrayList collection.

Brian
I want to call the play method on the ones in the ArrayList collection
Size_J
A: 

Your class attribute private Player p is not instantiated, so that when you call p.play() in the play() method, you get an NPE. Instantiate private Player p before you call p.play().

Kaleb Brasee
+1  A: 

Your code needs a LOT of work, but this gets you past the NPE. The problem is that your private data member Player is never initialized.

package psychic;

import java.util.*;

public class PsychicGameMore
{
    private ArrayList<Player> players;
    private int orginalNumber;

    public static void main(String[] args)
    {
        PsychicGameMore game = new PsychicGameMore();
        game.play();
    }

    public PsychicGameMore()
    {
        int n;
        Scanner s;
        s = new Scanner(System.in);
        System.out.println("How many players will there be?, Please enter a number");
        n = s.nextInt();
        players = new ArrayList<Player>(n);

        //for loop to create n number of Players
        for (int i = 0; i < n; i++)
        {
            Player p;
            p = new Player();


            players.add(p);
        }

        orginalNumber = 0;
    }

    public void play()
    {
        Random myRandom;
        myRandom = new Random();

        boolean hasWon = false;
        do
        {
            orginalNumber = myRandom.nextInt(6) + 1;
            System.out.println("The computer has choosen the number " + orginalNumber);
            for (Player player : players)
            {
                player.play();

                if (orginalNumber == player.getGuessedNumber())
                {
                    System.out.println(player.getName() + " has won!!!!!!!!!!!!!!!!");
                    hasWon = true;
                }

            }
        } while (!hasWon);
    }
}
duffymo
Thank you, I have beautified my code. I know it was hard to read. I will work on posting better indented code. What I was not getting was the for(Player player : players) loop. Thanks
Size_J
So why not vote the answer up if you've accepted it?
duffymo
I have now, thanks for the help again
Size_J