Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with NullPointerException

Status
Not open for further replies.

chrisxon

Technical User
Jul 21, 2006
2
CA
I am having a difficulty with a piece of code, it is causing a nullpointerexception and its because my variable is being returned as null, help anyone?

public void newGame()
{
this.theDeck = new Deck();
this.piles = new Pile[Constants.NUM_PILES];
Card tempCard2;

for (int i=Constants.FIRST_LAYOUT_PILE; i <
Constants.NUM_LAYOUT_PILES ; i++)
{
for (int x = 0; x < Constants.initalCardsOnPile(i); x++)
{
tempCard2 = theDeck.dealACard();
this.piles.addACard( tempCard2 );
}
}
}


public void addACard(Card c)
{
cards.addElement(c);
}


public Card dealACard()
{
return cards[curCardIndex++];
}
 
No way to know without the stacktrace or any hint of where it's happening.

Cheers,
Dian
 
I can't see where the 'cards' reference is declared / initialised. What is it? You seem to be using it both like an array and like a List instance. It can't be both.

Tim
 

public class Deck
{
Card[] cards;
int curCardIndex;
public Deck()
{

int i = 0;
cards = new Card[ Constants.NUM_CARDS_IN_DECK ];
curCardIndex = 0;

//Loops to the amount of cards in a deck
for(int iSuit = 0; iSuit < Constants.LAST_SUIT; ++iSuit)
for(int iRank = 0; iRank < Constants.NUM_CARDS_PER_SUIT ; ++iRank)
{
//Places a random card from the ordered deck into the mixed deck
this.cards[i++] = new Card(iRank,iSuit);
}

//Calls the shuffel function to mix up the ordered deck
shuffle();

}

/*Takes the ordered deck created in the deck() method and
mixes it into a new deck, named mixedDeck, and pus them into a
random order.*/
public void shuffle()
{
int randomNumA = 1;
int randomNumB = 1;
Card tempCard;


//Loops to the amount of cards in a deck
for(int x = 0; x < Constants.NUM_CARDS_IN_DECK; ++x)
{
//Creates a random number between 0 and 51
randomNumA = (int) (Math.random()*(Constants.NUM_CARDS_IN_DECK));
randomNumB = (int) (Math.random()*(Constants.NUM_CARDS_IN_DECK));
//Places a random card from the ordered deck into the mixed deck
tempCard = cards[randomNumA];
cards[randomNumA] = cards[randomNumB];
cards[randomNumB] = tempCard;

}
}

/*Deals a card into the play field*/
public Card dealACard()
{
return cards[curCardIndex++];
}

}


When the line "cards[curCardIndex++} executes it is being returned as null. I am not sure why it is doing that
 
your curCardIndex isn't private, so it might be modified outside your class, or you call dealACard() more often, than cards are in your stack.

By the way: Stack.
Why don't you use a stack?

seeking a job as java-programmer in Berlin:
 
change your incriment operators from prefix to postfix.
Code:
//Loops to the amount of cards in a deck
for(int iSuit = 0; iSuit < Constants.LAST_SUIT; iSuit++)
   for(int iRank = 0; iRank < 
      Constants.NUM_CARDS_PER_SUIT;iRank++){
      //Places a random card from the ordered deck 
      //into the mixed deck
      this.cards[i++] = new Card(iRank,iSuit);
   }
 
By the way: Stack.
Why don't you use a stack?
I'd imagine you wouldn't use a stack because when you think of a deck of cards as an object you might want to pick up the stack, split the stack in two, and shuffle from the bottom.
 
Why don't you just hold all your Card instances in a List (eg. ArrayList) and use the java.util.Collections.shuffle(List) method to shuffle them? Much easier than an array.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top