0

I have an ArrayList which stores 52 card objects like so:

 public class Pack

{
   private ArrayList<Card> cards;
   private Card RandomCard;


   public static void main(String[] args) {
    ArrayList<Card> cards = new ArrayList<Card>();

    cards.add(new Card('C','A'));
    cards.add(new Card('C','2'));
    cards.add(new Card('C','3'));

etc..

I also have this method which generates a random number to grab a random object from my ArrayList.

public Card getRandomCard()

{
   int number = (int) (Math.random() * 52.0);
   return RandomCard;
}

This compiles but when I test it I get returned 'null'. I have to include this method! Any suggestions?

4
  • Is this sudo code? Because RandomCard is definitely null if this is your actual code. If it's sudo, have you tested to make sure that the random number is within the array range? Also, if you want an int, why do you say 52.0? Wouldn't 52 be better? Commented Aug 14, 2010 at 0:28
  • When do you assign RandomCard? Commented Aug 14, 2010 at 0:30
  • Why is it definitely null? the code is java. I don't want an int I want a card object to be returned.I figured the int was part of creating a random generator alogrithm to create a random number and use this to grab a random Card object... :S Commented Aug 14, 2010 at 19:54
  • It's null because you never set RandomCard to anything. Commented Aug 15, 2010 at 9:31

2 Answers 2

4

try

return cards.get(number);

instead of

return RandomCard;

The last statement returns RandomCard var, which isn't assigned anywhere.

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

Comments

4

Try this:

public Card getRandomCard() {
    return cards.get((int) (Math.random() * cards.size()));
}

Alternative using Random:

public Card getRandomCard() {
    Random gen = new Random(); // Make this an instance variable instead.
    return cards.get(gen.nextInt(cards.size()));
}

Edit:

Your original code tries to initialize an ArrayList of Cards in the main method (line 9), which is a completely different ArrayList from your ArrayList in Pack. This means the ArrayList in Pack (line 4) remains null, which explains your NullPointerException described below. Replace your code with this:

public class Pack {

    private ArrayList<Card> cards;
    private Random random;

    public Pack() {
        random = new Random();
        cards = new ArrayList<Card>();

        cards.add(new Card('C','A'));
        cards.add(new Card('C','2'));
        cards.add(new Card('C','3'));
        // And so on..
    }

    public Card getRandomCard() {
        return cards.get(random.nextInt(cards.size()));
    }

    public static void main(String[] args) {
        Pack pack = new Pack();
        Card randomCard1 = pack.getRandomCard(); // Here is your random card.
        Card randomCard2 = pack.getRandomCard(); // Here is another random card.
    }
}

2 Comments

I tried the top method and recieve an error "java.lang.NullPointerException at Pack.getRandomCard(Pack.java:83) I'm getting worried now as I can't figure this out. Basically I have my Card class, this has 2 char attributes to hold the Card's suit and value. My pack class holds an ArrayList<Card>, which stores details of all 52 cards (You can see from my orginal post). My problem is I can't seem to grab a random Card from the ArrayList and I have to do so using the method I was given. The randomCard is then used as the current target card for my game. So the randomCard changes on every game.
The code you posted has an error in it. I'll edit my post above and explain.

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.