I am pretty new to java and I am trying to write a simple game but I am getting this error in my program "java.lang.NullPointerException" couldn't figured out why I am getting this so I am posting my code here.
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("How many players?:");
int t = console.nextInt();
Player p[] = new Player[t];
for(int i=0; i<t; i++){
System.out.println("Player " + i+1 + " enter your name:");
p[i].player_name = console.nextLine();
System.out.println(p[i].player_name + " Easy(1), Medium(2), Hard(3), enter one integer");
p[i].number = console.nextInt();
System.out.println("Player " + i+1 + "name: " + p[i].player_name + "your integer is " + p[i].number);
}
}
And this is my Player class
public class Player {
public String player_name;
public int number;
public Player() {
this.player_name = " ";
this.number = 0;
}
}
p[i] = new Player();