I am currently trying to make a java card game but am having trouble setting the card. I am taking in values such as 2H 3D 4S 5C 6H
in the main function. I am trying to put these values into my Card class but when I try to set my rank I get a nullpointerexception error.
I am new to java programing and can not figure out why this is happening. Any suggestions? Am I not allowed to make an array of Cards?
public class Game {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
String[] player1arr = new String[5];
String[] player2arr = new String[5];
Card[] player1 = new Card[5];
Card[] player2 = new Card[5];
for(int i = 0; i < 5; i++){
player1arr[i] = sc.next();
char first = player1arr[i].charAt(0);
int rank = Character.getNumericValue(first); //error
player1[i].setRank(rank);
}
for(int i = 0; i < 5; i++){
player2arr[i] = sc.next();
System.out.println(player2arr[i]);
}
}
}
class Card{
private int rank;
private char suit;
public int getRank(){
return rank;
}
public void setRank(int r){
rank = r;
}
}