When running next code I get a error “Exception in thread “main” java.lang.NullPointerException“
I think it has to do with the filling of deck[i2]
What i try to do is adding a value each time the for loop is run.
Can someone tell me what’s going wrong?
public class Deck {
private Cards cards;
private String[] suits;
private String[] ranks;
private String[] deck;
private int i2;
public void Deck() {
//Instantiate class Cards
cards = new Cards();
//run method to get cards / suits
suits = cards.getSuits();
ranks = cards.getRanks();
//Build an array (deck) and fill it with all possible cards
i2 = 1;
for (int i = 0; i < suits.length; i++) {
//Run through ranks
for (int i1 = 0; i1 < ranks.length; i1++) {
deck[i2] = suits[i] + ranks[i1];
2++;
}
}
}
}
You never initialized ‘deck’. Should be something like: