i have this code.
numberPlayers = 2;
players = new Player[numberPlayers];
setName();
setHand();
private static void setName() {
for(int i=0;i<numberPlayers;i++) {
out.print("Player "+ i + " name: ");
players[i].setName(in.nextLine());
out.println();
}
}
private static void setHand() {
for(int i=0;i<numberPlayers;i++) {
out.print("Player "+ i+1 + " hand: ");
players[i].setName(in.nextLine());
out.println();
}
}
but this gives me error:
ROCK-PAPER-SCISSORS GAME
NOTE: hand input will loop until user(s) input(s) valid hand values
VALID HANDS: R-Rock, P-Paper, S-ScissorsSelect your option below:
[1] Player vs Player
[2] Player vs Computer 1
Exception in thread "main" java.lang.NullPointerException
at RPS2.Game.setName(Game.java:61)
at RPS2.Game.main(Game.java:37)
Player 0 name: Java Result: 1
what's wrong? i want to instantiate an array object named Player with two elements.