0
Error: Exception in thread "main" java.lang.NullPointerException
at Deck.toString(Deck.java:83)
at DeckDriver.main(DeckDriver.java:25)

I'm completely lost as to why i'm getting this error message. The d.toString is supposed to display 52 lines of code describing the Deck d.

Deck class

import java.util.Random;
public class Deck
{
    private Card[] deck;
    private int nextCard;
    Face face;
    Suit suit;


    /**
     * Default Constructor        
     *
     * <hr>
     * Date created: Feb 17, 2014 
     *
     * 
     */
    public Deck()
    {
        nextCard = 0;
        deck = new Card[52];
        int iCount;
        for(iCount=0; iCount<52; iCount++)
        {
            Card c = new Card(iCount);
        }
    }


    /**
     * Copy Constructor        
     *
     * <hr>
     * Date created: Feb 17, 2014 
     *
     * 
     * @param existingDeck
     */
    public Deck(Deck existingDeck)
    {
        int i;
        for(i=0;i<52;i++)
        {
            this.deck[i] = existingDeck.deck[i];
        }
    }

    /**
     * toString         
     *
     * <hr>
     * Date created: Feb 17, 2014 
     *
     * <hr>
     * @return
     * @see java.lang.Object#toString()
     */
    public String toString()
    {
        int iCount = 0;
        String description = "";
        for(iCount=0; iCount<52;iCount++)
        {
            description += deck[iCount].toString();
        }
        return description;
    }

    /**
     * Shuffles the deck       
     *
     * <hr>
     * Date created: Feb 17, 2014
     *
     * <hr>
     */
    public void shuffle()
    {
        Random r = new Random();
        nextCard = 0;
        int i;
        for(i=0;i<52;i++)
        {
            int x = r.nextInt(52);
            Card c = new Card();
            c=deck[x];
            deck[x]=deck[i];
            deck[i]=c;
        }
    }

    /**
     * Deals individual card.        
     *
     * <hr>
     * Date created: Feb 17, 2014
     *
     * <hr>
     * @return
     */
    public Card dealACard()
    {
        Card c;
        c=deck[nextCard];
        nextCard++;

        return c;
    }

    public String dealAHand(int handSize)
    {
        int i;
        String hand="";
        for(i=0;i==handSize;i++)
        {
            hand+="" + dealACard();
        }
        return hand;
    }
}

DeckDriver class

public class DeckDriver
{
    public static void main(String[]args)
    {
        Deck d = new Deck();
        System.out.print(d.toString()); //(this is the DeckDriver.main(DeckDriver.java:25))
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

4 Answers4

1

There is no object added to array in default constructor. You should initialize every field of the array the same way as you do in the other constructor.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
1

These lines

for(iCount=0; iCount<52; iCount++)
{
    Card c = new Card(iCount);
}

don't have much effect. You're not storing the new Card object anywhere, so it will get thrown away by the garbage collection process. They should probably say something line this.

for(iCount=0; iCount<52; iCount++)
{
    deck[iCount] = new Card(iCount);
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

Best way to prevent against NPE is to use Apache Commons Lang StringUtils.

In this case, it looks like StringUtils.defaultString(deck[iCount]) would be the way to go. If it's null, defaultString will return "" (an empty string).

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#defaultString%28java.lang.String%29

If you decide to not use StringUtils, the best practice is to always check for null before doing .toString().

Matt
  • 477
  • 1
  • 5
  • 19
0

You're initializing your array correctly, but not assigning new Cards to it in your constructor:

public Deck()
{
    nextCard = 0;
    deck = new Card[52];
    int iCount;
    for(iCount=0; iCount<52; iCount++)
    {
        Card c = new Card(iCount);
    }
}

You instantiate each card ... then throw it away. This means every element in your deck array is null as that is the default value.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161