开发者

Java NullPointerException When I call a method on a custom class

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;
    priv开发者_JAVA技巧ate 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.


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.


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);
    }
}


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.


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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜