0

The error occurs in the last line of this class where I'm trying to print the number of the first card in the deck and I'm not really sure why.

    public class CardTricks {
    public static void main (String[] args){

        Deck newdeck = new Deck();
        newdeck.construct();
        newdeck.shuffle();
        System.out.println(newdeck.deck[0].Number);

    }
}

This is the class for the card with the main purpose to give the card a suite attribute and a number attribute.

public class Card {
    String Suite;
    int Number;
}

This is the deck class, functions in this class are used to create the deck and shuffle it.

public class Deck {
    Card[] deck;

    public void construct(){

        deck = new Card[52];

        String[] possuite = new String[4];
        possuite[0] = "Hearts";
        possuite[1] = "Diamonds";
        possuite[2] = "Clubs";
        possuite[3] = "Spades";

        int x = 0;

        while (x < 4){
            String suite = possuite[x];
            x++;
            int number = 1;
            System.out.println(suite);

            while (number < 14){
                deck[number-1] = new Card();

                deck[number-1].Suite = suite;
                deck[number-1].Number = number;
                number++;
           }
        }
    }

    public void shuffle(){
        int x;
        int y;
        int z = 0;

        while (z < 10000){
            x = (int)(Math.random()*52);
            y = (int)(Math.random()*52);
            Card a = deck[y];
            Card b = deck[x];
            deck[x] = a;
            deck[y] = b;
            z++;
        }

    }
}
4
  • post full exception Commented Oct 19, 2018 at 8:56
  • You propably have null elements in your deck array. Print the whole array before and after the shuffle to identify where the null value is. Commented Oct 19, 2018 at 8:58
  • number < 14 -> only first 13 cards will be generated; after shuffling eventually deck[0] is one of the ones that was not created. Your loops are confusing, first one starts with index 0, the second with 1 - you should stick with one standard. Also I would suggest using a third index for the deck, that is, one for suite, second for number and 3rd to access the deck...and maybe a for loop is more readable. Commented Oct 19, 2018 at 9:07
  • @potato if you found one of the answers useful please close the question and mark it as answered. Commented Oct 19, 2018 at 9:34

2 Answers 2

1

You are only populating the first 13 places in the deck and overriding them each time you produce a new suite.

what you should do: (note that I have moved the x++ to the bottom)

    while (x < 4){
        String suite = possuite[x];
        int number = 1;
        System.out.println(suite);

        while (number < 14){
            int cardPos = (number-1) + (13*x);
            deck[cardPos] = new Card();


            deck[cardPos].Suite = suite;
            deck[cardPos].Number = number;
            number++;
       }
       x++;
Sign up to request clarification or add additional context in comments.

Comments

0

newdeck.deck[0] is null because you shuffle all 52 field elements while you have initialized element 0 to 12 only.

Shuffle places a null element to position deck[0].

// this creates a new array with elements 0 to 51 == null
deck = new Card[52];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.