Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4025486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:51:10+00:00 2026-05-20T10:51:10+00:00

I am a beginner in java programming. I am trying to recreate a simplified

  • 0

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");
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:51:11+00:00Added an answer on May 20, 2026 at 10:51 am

    Replace:

    TheCard[] theDeck = null;
    

    with:

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

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a beginner in programming. I've just made a program called Guessing Game. And
I'm beginner in Java, I'm trying to compile some small program, can somebody explain
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I'm trying
I am a complete JSP beginner. I am trying to use a java.util.List in
As a complete beginner with no programming experience, I am trying to find beautiful
I'm a beginner in practical real-world java programming. I don't really understand these basic
I'm a beginner in programming web apps with Java EE. How do you name
I'm a beginner in Java programming language, and I have a problem that I
I am a beginner in programming with Java Android. I am currently confused with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.