Hi,
I am having trying to model a deck(s) of cards in Java and am having trouble with an array. I want to get a 2d array with the first set being the value of the card and the second being the suit.
If i leave it as a 1d array, just adding the card values, everthying works fine but when I try to change to 2d all I get is Null.pointerException and ArrayIndexOutOfBounds errors.
public class decks { // instance variables - replace the example below with your own private int numberOfDecks; private int totalNoCards; private String cardArray[][]; private String oneDeck[] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"}; private String suits[] = {"Clubs","Spades","Hearts","Diamonds"}; /** * Constructor for objects of class decks */ public decks(int numberOfDecks) { // initialise instance variables this.numberOfDecks = numberOfDecks; this.totalNoCards = numberOfDecks * 52; cardArray = new String[0][totalNoCards]; } /** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public void addCards() { int counterOne = 1; int counterTwo = 0; int counterThree = 0; int counterFour = 0; while(counterOne <= numberOfDecks) { cardArray[0][counterTwo] = oneDeck[counterThree]; counterTwo++; counterThree++; if(counterThree > 12) { counterFour++; counterThree = 0; if(counterFour > 3) { counterFour = 0; counterOne++; } } } } }
I am sure there is something simple I am missing. Any help is much appreciated.
Thankyou