I'm trying to create a card game. It has 3 classes, "PlayingCard", "DeckOfPlayingCards" and "Game".
public class PlayingCard {
private String rank;
private String suit;
public void setRankAndSuit(String rank, String suit) {
this.rank = rank;
this.suit = suit;
}
public String getRankAndSuit() {
return rank + " of " + suit;
}
}
public class DeckOfPlayingCards extends PlayingCard {
// Array of playing card objects
static PlayingCard[] deck = new PlayingCard[2];
DeckOfPlayingCards() { // This is my line 11 BTW
deck[0].setRankAndSuit("1", "S");
deck[1].setRankAndSuit("2", "S");
}
}
public class Game extends DeckOfPlayingCards {
public static void main(String[] args) {
DeckOfPlayingCards newDeck = new DeckOfPlayingCards(); // This is my
// line 6
System.out.println(deck[0].getRankAndSuit());
}
}
Everything compiles fine, but when I run it, I get a Exception in thread "main" java.lang.NullPointerException at DeckOfPlayingCards.<init>(DeckOfPlayingCards.java:11) at Game.main(Game.java:6)
As I understand, this exception is thrown when I'm trying to access something which is null. I simplified the program to see where the problem is, but I still can't figure it out. I initialized the array of 2 elements before I called them, so I don't know what is wrong. Please help me