Passing objects as parameters[Java]
Author |
Message |
Cinjection
|
Posted: Sun Apr 08, 2007 12:02 pm Post subject: Passing objects as parameters[Java] |
|
|
Ok. I'm making a game of poker in Java. I have several class including one called Card. This clas contains card information about one card. So then in theory, there will be 9 Card objects in the game/per round. Thats fine.
Here's my problem, I need to pass the Card object around to do various things (checking for conbinations of cards, checking for duplicate card, etc). When I pass my object (or print it out), I get null. Clearly i'm doing something wrong. Here the one of the problem methods:
code: |
private void dealHands(){
Card [] handCards = new Card[4]; //Array to hold the four cards
boolean isDuplicate = true;
//The code block below ensures that duplicate cards have not been generated.
for (int c = 0; c < 4; c++){ //Loops through the handCards array.
isDuplicate = true;
while(isDuplicate){ //Loops through each card until a non-duplicate is selected
if ( gameTable.checkDuplicate( handCards[c] ) ){ //Checks to see if the card generated has already been drawn
handCards[c].regenerateValues();
} else {
gameTable.updateDuplicateList( handCards[c] );
isDuplicate = false;
} //End isDupilicate if check
}//End isDuplicate while check
} //End handCards Loop
//Assigns the cards to the players. By this point, cards generated are non-duplicates
user.addCardsToHand( handCards[0], handCards[2] );
theBot.addCardsToHand( handCards[1], handCards[3] );
//Add the AI hand cards to AI database (Hippcampas/brain)
brain.addAICards( handCards[1], handCards[3] );
//Output the user's cards
System.out.println("The two cards you draw are:");
System.out.println( handCards[0].readYourself() );
System.out.println( handCards[2].readYourself() );
}//End dealHands
|
The card values (suit and numbered value) gets determined on creating by the constructer. If it's a duplicate, I call a method in Card to regenerate the card values.
Unfortunetly, every time I print out or pass handCards[c], i get a null value. And of course when i'm playing with a null object, i get a null pointer exepetion. The call to checkDuplicate always has a null parameter.
Could someone please explain to me what i'm doing wrong. Thanks a lot.
p.s; some of you (very few) might remeber me making this project with a Neural AI. Well that's still in the making, only I have to get the whole basic game play down. Current project LOC: ~1,500 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Sun Apr 08, 2007 1:07 pm Post subject: RE:Passing objects as parameters[Java] |
|
|
INDENT YOUR CODE!!
other than that, I'm guessing that your objects are not being initialized before that loop, in which case your pointers are indeed NULL. |
|
|
|
|
|
PaulButler
|
Posted: Sun Apr 08, 2007 1:18 pm Post subject: RE:Passing objects as parameters[Java] |
|
|
The Cards aren't being constructed. It looks like you are relying on this code:
Java: |
Card [] handCards = new Card[4]; //Array to hold the four cards
|
to create the cards, but it does not call the constructors on each card, it gives each card a null value until you construct it. This should fix it:
Java: |
for (int c = 0; c < 4; c++){ //Loops through the handCards array.
/* The following line is what I added */
handCards[c] = new Card();
isDuplicate = true;
while(isDuplicate){ //Loops through each card until a non-duplicate is selected
if ( gameTable.checkDuplicate( handCards[c] ) ){ //Checks to see if the card generated has already been drawn
handCards[c].regenerateValues();
} else {
|
Edit: Clayton beat me to it. |
|
|
|
|
|
Cinjection
|
Posted: Sun Apr 08, 2007 6:22 pm Post subject: Re: RE:Passing objects as parameters[Java] |
|
|
Clayton @ Sun Apr 08, 2007 1:07 pm wrote: INDENT YOUR CODE!!
other than that, I'm guessing that your objects are not being initialized before that loop, in which case your pointers are indeed NULL.
1.Code was indented but i copy and pasted to post from a different forum so indenting got fucked.
2. So I can't create objects like that. I have to loop? Well that's cool. Thanks a lot fellers! |
|
|
|
|
|
|
|