Computer Science Canada Passing objects as parameters[Java] |
Author: | Cinjection [ 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:
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 |
Author: | Clayton [ 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. |
Author: | PaulButler [ 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:
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:
Edit: Clayton beat me to it. |
Author: | Cinjection [ 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! ![]() |