4

I am a beginner in java programming. I am trying to recreate a simplified version of the card game war. I ran my program which is posted below and it came back with this error:

Exception in thread "main" java.lang.NullPointerException

at cardgame.BuildDeck(cardgame.java:36)

at cardgame.main(cardgame.java:60)

I have been trying to solve this issue on my own through research, but I could not solve it. I was wondering if anyone can help me. If you do need any other information about my program, please just ask. Thanks in advance!

-FGxMatta

public class cardgame 
{
static class TheCard
{
    // Java getter & setter
    private String CardName;
    private int CardRank;
    private int Chosen;

    public TheCard(int rank, String name)
    {
        this.CardName = name;
        this.CardRank = rank;
        this.Chosen = 0;
    }
}


@SuppressWarnings("null")
private static TheCard[] BuildDeck()
{
    TheCard[] TheDeck = null;
    String[] Cards =  {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    String[] Suits = {"Spades","Hearts","Diamonds","Clubs"};
int[] Rank = {2,3,4,5,6,7,8,9,10,11,12,13,14};
    int cardnumber = 0;
    for (int i = 0; i < Cards.length; i++)
    {
        for (int j = 0; j < Suits.length; j++)
        {
            String deckcard = Cards[i];
            String suitcard = Suits[j];
            String cardname = deckcard + "-" + suitcard;
            TheDeck[cardnumber] = new TheCard(Rank[i], cardname);  
            cardnumber++;
        }
    }
    return TheDeck;
}
private static TheCard GetRandomCard(TheCard[] OrderedDeck) 
{
    TheCard thecard;
    int random = (int) (51*Math.random ());
    thecard = OrderedDeck[random];
    if (thecard.Chosen == 0 )       // if available... 
    {
        thecard.Chosen = 1;         // mark it taken...
        return thecard;
    }
    else
    {
        return GetRandomCard(OrderedDeck);
    }
}

public static void main(String args[])
{
    TheCard[] OrderedDeck = BuildDeck();
    System.out.println ("Welcome, Prepare for War!");
    int decksize = OrderedDeck.length;
    int player1wincount = 0;
    int player2wincount = 0;
    int tiecount = 0;
    for (int cardcount = 0; cardcount < decksize;)
    {
        TheCard Player1 = GetRandomCard(OrderedDeck);
        cardcount++;
        TheCard Player2 = GetRandomCard(OrderedDeck);
        cardcount++;
        System.out.println ("Player 1's card is: " + Player1.CardName);
        System.out.println ("Player 2's card is: " + Player2.CardName);
        if (Player1.CardRank > Player2.CardRank)
        {
            System.out.println("Player 1 wins this hand");
            player1wincount++;
        }
        if (Player1.CardRank < Player2.CardRank)
        {
            System.out.println("Player 2 wins this hand");
            player2wincount++;
        }
        if (Player1.CardRank == Player2.CardRank)
        {
            System.out.println("Player 1 and Player 2 played the same valued card");
            tiecount++;
        }
    }
    System.out.println ("Player 1 won " + String.valueOf(player1wincount) + " hands");
    System.out.println ("Player 1 won " + String.valueOf(player2wincount) + " hands");
    System.out.println ("There were " + String.valueOf(tiecount) + " ties");
}
}
Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
FGxMatta
  • 45
  • 1
  • 1
  • 4
  • 2
    If you want the compiler to give you a big hint as to where the problem is, remove the line `@SuppressWarnings("null")` You will then be told exactly where you are trying to access a null object. – DaveJohnston Mar 04 '11 at 17:22
  • 4
    I'm going to assume you're using Eclipse and that Eclipse suggested the `@SuppressWarnings("null")`. Remove that line and fix the code that causes the warning. The warning is to indicate a problem. Don't use `@SuppressWarnings` until you know the consequences. The warning was trying to prevent the exact error you received. – Jonathon Faust Mar 04 '11 at 17:23

5 Answers5

11

Replace:

TheCard[] theDeck = null;

with:

TheCard[] theDeck = new TheCard[Cards.length * Suits.length];

and move it to below the declarations for Cards and Suits.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
4

Null Pointer Exception is a situation in code where you try to access/ modify an object which has not been initialized yet. It essentially means that object reference variable is not pointing anywhere and refers to nothing or ‘null’. A simple example can be:

package au.com.copl;
public class Demo{
    public static void main(String[] args) {
        String d = null;
        System.out.println(d.toString()); // d is un-initialized and is null
    }
}
Z.U.H
  • 325
  • 2
  • 13
1

right here

TheDeck[cardnumber] = new TheCard(Rank[i], cardname);  

you never initialized TheDeck. You probably want something like

TheDeck = new TheCard[52];

before you start putting things in the array.

as a note, java convention is that variable names be camel cased. So "CardName" should be cardName. Just a convention.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
1

TheCard[] theDeck = null; ?

dont you need to inialize it?

0

You should also use new in main method:

TheCard[] OrderedDeck = BuildDeck();

should be replaced by:

TheCard[] OrderedDeck = new BuildDeck();
PatNowak
  • 5,721
  • 1
  • 25
  • 31