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

  • SEARCH
  • Home
  • 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 8837901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:50:53+00:00 2026-06-14T09:50:53+00:00

I’m working on a solitaire counting program. I already have the main program working

  • 0

I’m working on a solitaire counting program. I already have the main program working but when I try to implement my own class I get an NullPointerException error on line 19 (whenever it reaches c.getRank).

Note that I first created my main program while importing a class called CardDeck that has all the functions I need for it to work but now I’m supposed to create my own class that does the exact same thing. (Note that I don’t have access to the imported CardDeck class).

Here is the main code:

import se.lth.cs.ptdc.cardGames.Card;

public class Patiens {
public static void main(String[] args) {
    double good = 0;
    double bad = 0;
    double result = 0;

    for (int a = 0; a < 1000000; a++) {
        CardDeck deck = new CardDeck();
        deck.shuffle();
        double fail = 0;
        while (deck.moreCards()) {

            for (int i = 1; i <= 3 && deck.moreCards(); i++) {

                Card c = deck.getCard();

                if (i == 1 && c.getRank() == 1) {
                    fail++;
                }

                if (i == 2 && c.getRank() == 2) {
                    fail++;
                }

                if (i == 3 && c.getRank() == 3) {
                    fail++;
                }
            }
        }
        if (fail >= 1) {
            bad++;      
        }
        else{
            good++;
        }
    }
    System.out.println("Good: " + good + " Bad: " + bad);
    result = good / bad;
    System.out.println("Result= " + result);
}

}

What it does is count the probability that my deck will finish successfully:

It’s counting 1-2-3, 1-2-3 while at the same time drawing a card. Now IF the card happens to be an ACE when it counts “1” the current deck will fail. Same goes for a card of rank 2 while the program counts “2” etc. The probability that it will finish without failing once is 0.8% .

Here is the CardDeck class I’m creating:

import se.lth.cs.ptdc.cardGames.Card;

import java.util.Random;

public class CardDeck {
    private Card[] cards;
    private int current;
    private static Random rand = new Random();

    public CardDeck() {
        cards = new Card[52];
        for(int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
            for (int i = 0; i < 13; i++) {
                cards[i * suit] = new Card(suit, i);
            }
        }
        current = 0;
    }

    public void shuffle() {
        Card k;
        for(int i = 1000; i > 0; i--) {
            int nbr = rand.nextInt(52);
            int nbr2 = rand.nextInt(52);
            k = cards[nbr2];
            cards[nbr2] = cards[nbr];
            cards[nbr] = k;
        }
    }

    /**
     *Checks for more cards
     */
    public boolean moreCards() {
        if(current > 51) {
            return false;
        } else {
            return true;
        }
    }

    /**
     *Draws the card lying on top.
     */
    public Card getCard() {
        return cards[current++];

    }
}

Here is the import se.lth.cs.ptdc.cardGames.Card; If needed, It is the class that creates the cards.

package se.lth.cs.ptdc.cardGames;

public class Card {
    public static final int SPADES = 1;
    public static final int HEARTS = SPADES + 1;
    public static final int DIAMONDS = SPADES + 2;
    public static final int CLUBS = SPADES + 3;

    private int suit;
    private int rank;

    public Card(int suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public int getSuit() {
        return suit;
    }

    public int getRank() {
        return rank;
    }
}

(Note that I’m not supposed to change the above class)

  • 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-06-14T09:50:54+00:00Added an answer on June 14, 2026 at 9:50 am

    Your problem is here:

    cards[i * suit] = new Card(suit, i);
    

    If you change this to:

    cards[i + ((suit - 1) * 13)] = new Card(suit, i);
    

    It will do what you expect.

    Two things to consider: firstly, arrays are zero-based, so your first card needs to be at index 0. Secondly, by multiplying by the suit, you will get multiples of that number, e.g.:

    • SPADES: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
    • HEARTS: 2, 4, 6, 8, 10 …
    • DIAMONDS: 3, 6, 9, 12 …
    • CLUBS: 4, 8, 12, 16 …

    So some elements will be filled more than once (12 is filled four times), and some (particular prime numbers > 13) elements (e.g. 23) will be null. In general, it’s probably enough to represent the index with another variable, like so:

    int cardIndex = 0;
    for (int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
        for (int i = 0; i < 13; i++) {
            cards[cardIndex++] = new Card(suit, i);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have been unable to fix a problem with Java Unicode and encoding. The
this is what i have right now Drawing an RSS feed into the php,

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.