-6

It keeps throwing this exception, but it shows up in between the output of the program that is so far operating exactly as I need it to. What could the issue be?

Alright so this is where it throws the exception: public class RunGame {

public static void main(String[] args) {
    //Create players
    Hand Player1 = new Hand(5);
    Hand Player2 = new Hand(5);
    Hand Player3 = new Hand(5);
    Hand Player4 = new Hand(5);

    Deck deck = new Deck();
    Card print = new Card(1,'c');

    deck.Shuffle();
    for (int i = 0; i <= 52; i++){
        print = deck.getCard(i); //**THROWS HERE**
        System.out.println(print.toString());
    }    

And then in my Deck class there's:

public class Deck {
private char suit;
private int value;
private Card [] deck = new Card[52];

public Deck(){ 

    int count = 0;
    for(int i = 1; i <= 4; i++) {
        if (i == 1)
        {
            suit = 'C';
        }
        else if (i == 2) {
            suit = 'D';
        }
        else if (i == 3)  {
            suit = 'H';
        }
        else {
            suit = 'S';
        }
        for (int x = 1; x <= 13; x++){
            deck[count] = new Card(x, suit);
            count++;
        }
    }
}

and

public Card getCard(int i){
    int v = deck[i].getValue(); //**AND HERE**
    char s = deck[i].getSuit();
    Card temp = new Card(v,s);
    return temp;
}

I just wanted to print the deck to make sure it was shuffling correctly. Everything prints out great but it still shows that. (My deck object is an array of 52 cards, and the getValue and getCard methods are in the Card class.)

007
  • 21
  • 3
  • 6
    You need to show code -- specifically the lines involved with the NPE. Otherwise you're asking us to guess about code not shown and our skills at clairvoyance is quite limited. – Hovercraft Full Of Eels Oct 15 '13 at 23:13
  • 1
    I would look at the stack trace and see where this happens. The issue is the code accesses a reference which is `null`. – Peter Lawrey Oct 15 '13 at 23:13
  • Please, paste the complete code for your `Deck` class. – Trein Oct 15 '13 at 23:36
  • See http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Raedwald Jan 09 '14 at 08:24

2 Answers2

1

I'm guessing deck.getCard(i) in your loop at some point returns null, and then when you try to call toString() from that null reference, the program crashes.

Maybe your loop is supposed to say i < 52 instead of <=? That should have caused an ArrayIndexOutOfBoundsException, though. It's really hard to tell without seeing the entire Deck class.

Martin Lehmann
  • 774
  • 8
  • 15
1

You've got 52 cards.
If you start counting from 0 (correct) you reach card #52 at count 51.
You count up to and including 52; this is incorrect because element 0 needs to be included in the count.
A simple off-by-one-error.

Change the code to:

for (int i = 0; i < 52; i++){
    print = deck.getCard(i); //No more exception
Johan
  • 74,508
  • 24
  • 191
  • 319
  • ..... I'm beyond embarrassed, I've been staring at this for like half and hour now and didn't catch it. Thanks soooo much. – 007 Oct 15 '13 at 23:35
  • @Mick, the joker put you off track, it doesn't want to be left out, so it throw the exception. – Johan Oct 16 '13 at 00:17