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.