1

I am trying to create mutliple methods which all use data from the same array. The method createDeck works fine. But for the other two methods, they just return null. Is there a way to fix this?

class Deck
{
    private static String[] suit = {"Spades", "Diamonds", "Clubs", "Hearts"};
    private static String[] rank = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", 
                                     "King", "Ace"};
    private static String[] deck = new String[52];

    // create deck
    static void createDeck()
    {
        for (int i = 0; i < deck.length; i++)
        {
            deck[i] = rank[i % 13] + " of " + suit[i / 13];
            System.out.println(deck[i]);
        }
    }

    // shuffle deck
    static void shuffleDeck()
    {
        // shuffle the deck
        for (int i = 0; i < deck.length; i++)
        {
            int index = (int) (Math.random() * deck.length);

            String temp = deck[i];
            deck[i] = deck[index];
            deck[index] = temp;
        }

        // print the shuffled deck
        for (String u : deck)
        {
            System.out.println(u);
        }
    }

    static void findTop()
    {
        System.out.println(deck[0]);
    }

}
6
  • 1
    you already are using your array(s) in several methods. I don't see the issue here? How and in which order do you call them? Commented Dec 19, 2019 at 11:49
  • 2
    You need to call createDeck before you call other methods as until you do that, your deck array is filled with nulls Commented Dec 19, 2019 at 11:50
  • the other methods are void - i can't believe that they return null - what exactly is the problem? You can use the other two only after createDeck has been run. Commented Dec 19, 2019 at 11:50
  • @Grisgram she means that she gets a NPE when she tries to work with decks[i] Commented Dec 19, 2019 at 11:53
  • 1
    @Stultuske Printing null doesn't generate a NPE. Most likely OP refers to "null" being printed in the console Commented Dec 19, 2019 at 12:05

2 Answers 2

1

One way to solve this is to directly fill the array using a static initalizer which gets called automatically.

Just add this code block after the decalration of the array at the beginning of the class

private static String[] deck = new String[52];

static {
    for (int i = 0; i < deck.length; i++)
    {
        deck[i] = rank[i % 13] + " of " + suit[i / 13];
        System.out.println(deck[i]);
    }
}

And of course remove the method createDeck since it is no longer needed. The following code will now be executed correctly and printing values from the array

public static void main(String[] args) {
    Deck.findTop();
    Deck.shuffleDeck();
}

See this question for more info on static initializer

Sign up to request clarification or add additional context in comments.

Comments

0

If it is used in your object, you can put the Array inside the constructor of an object.

public class Deck {
   String[] deck;
   public Deck(){  
       this.deck = new String[52];
   }
}

1 Comment

Please add further details to expand on your answer, such as working code or documentation citations.

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.