6 Replies - 1410 Views - Last Post: 16 August 2011 - 04:45 AM Rate Topic: -----

#1 jp612  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-August 11

Null pointer exception when picking random elements from arraylist

Posted 16 August 2011 - 12:19 AM

Hi,

I'm trying to randomly pick 52 "card" objects from an arraylist and put them into a 2D array.
So I wrote the following code:

private Card[][] getRandomCardsArray() {
               ArrayList<Card> pack = createThePack();
	       Card[][] cards = new Card[NUMBER_OF_ROWS][NUMBER_OF_COLS];
			
			for(int i = 0; i < cards.length; i++) {
				for(int j = 0; j < cards[i].length; j++) {
					int rand = (int)(Math.random() * 51);
					cards[i][j] = pack.get(rand);
					pack.remove(rand);
					setupCardPosition(cards[i][j],i,j); //this aids gui
														
				}
			}
			
			
				
		return cards;
	}




But of course i got a Null pointer exception in the arraylist since I'm taking out elements and I'm re-picking the same cards. I'm not sure how to fix this since I'm very new to arraylists. I have googled around but haven't found anything that works yet.

I don't get it since I'm using this:

pack.remove(rand);



doesn't that remove the index entry from the arraylist and everything?

Any help would be greatly appreciated :smile2:

This post has been edited by jp612: 16 August 2011 - 12:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Null pointer exception when picking random elements from arraylist

#2 andy_pleasants  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 122
  • Joined: 08-July 10

Re: Null pointer exception when picking random elements from arraylist

Posted 16 August 2011 - 01:16 AM

Can I see your createThePack method please?

Also just something that may be a bit easier for you, the Collections class has a shuffle method that will shuffle a list for you. So what you could do is shuffle the list, and then take out x number of cards in a simple loop.
Was This Post Helpful? 0
  • +
  • -

#3 jp612  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-August 11

Re: Null pointer exception when picking random elements from arraylist

Posted 16 August 2011 - 01:24 AM

Ah, sure thing. This is the createThePack() Method.

private ArrayList<Card> createThePack() {
		ArrayList<Card> theCards = new ArrayList <Card> (NUMBER_OF_CARDS);
		int suitNum = CLUBS;
		int cardValue = 0;

		for (int i=0; i < TOTAL_NUMBER_OF_CARDS; i++) {
			theCards.add(new Card(suitNum, cardValue));

			if( cardValue == CARDS_IN_EACH_SUIT - 1) {
				suitNum++;
			}

			cardValue = (cardValue + 1) % CARDS_IN_EACH_SUIT;		
		}

		return theCards;
	}



when a card object is created it has a suit number starting from 0 and a value i.e the face value also starting from 0.

So the shuffle method will move everything back so the empty index slot is filled? I'll check it out now in the API. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 andy_pleasants  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 122
  • Joined: 08-July 10

Re: Null pointer exception when picking random elements from arraylist

Posted 16 August 2011 - 03:36 AM

No what the shuffle method does is rearrange elements in an a collection. The problem with random things are that a random sequence can repeat. However, in a deck of cards you do not want a true random sequence, what you actually want is the sequence shuffled.

What I'd do is something like the following:

List<Card> cards = createThePack();  //This creates your pack of cards

Collections.shuffle(cards);  //This shuffles the cards (like a dealer would)

int x = 0;  //This is just a counter over the collection of cards

for(int i = 0; i < NUMBER_OF_ROWS; i++)
{
     for(int j = 0; j < NUMBER_OF_COLS; j++
     {
          cardArray[i][j] = cards.get(x++);  //Get the cards and increment the counter
     }
}



This way you fill each cell of the 2D array with a distinct card, and I think it better simulates how cards are shuffled.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Null pointer exception when picking random elements from arraylist

Posted 16 August 2011 - 03:57 AM

it is not

int rand = (int)(Math.random() * 51);

but

int rand = (int)(Math.random() * (pack.size() - 1));

as you remove the element from the ArrayList. Actually better to create a Random object and to perform the remove in the same operation

private Card[][] getRandomCardsArray() {
               ArrayList<Card> pack = createThePack();
               Random ran = new Random();
	       Card[][] cards = new Card[NUMBER_OF_ROWS][NUMBER_OF_COLS];
			
			for(int i = 0; i < cards.length; i++) {
				for(int j = 0; j < cards[i].length; j++) {
					int idx = ran.nextInt(pack.size();
					cards[i][j] = pack.remove(idx);
					setupCardPosition(cards[i][j],i,j); //this aids gui
											
				}
			}
			
			
				
		return cards;
	}




This post has been edited by pbl: 16 August 2011 - 06:11 AM

Was This Post Helpful? 0
  • +
  • -

#6 dremok  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 24
  • Joined: 22-April 09

Re: Null pointer exception when picking random elements from arraylist

Posted 16 August 2011 - 04:10 AM

View Postjp612, on 16 August 2011 - 01:19 AM, said:

I don't get it since I'm using this:

pack.remove(rand);



doesn't that remove the index entry from the arraylist and everything?


I think the problem is that ArrayList's Remove-method shifts any subsequent elements to the left.
Which means that the indices no longer are 0 to 51, so you're trying to grab elements with index outside of the ArrayList.
Was This Post Helpful? 0
  • +
  • -

#7 jp612  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 16-August 11

Re: Null pointer exception when picking random elements from arraylist

Posted 16 August 2011 - 04:45 AM

Ohh right. I have gotten it to work using both ways. The shuffle method picks out random cards every time. It was also true (I think) that the remove method shrunk the arraylist thats why I was getting the out of bounds exception so I have to -- the size max random number each time or just use the .size() method.

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1