This site requires JavaScript, please enable it in your browser!
Greenfoot back
AwesomeNameGuy
AwesomeNameGuy wrote ...

2011/7/21

Question about strings

AwesomeNameGuy AwesomeNameGuy

2011/7/21

#
I'm at my wit's end, for the life of me why doesn't this work? I'm working on a game, and there is a card object. Each card has a string for example an Ace has an "A", a two has a "2", and so on. The card has a method called getStr() which returns the string. When the world deals the player a card, it calls a method player.assignCard(card) which passes the card information to the player. The assign card method looks like this: Card playercard; String cardname = ""; public void assignCard(card) { playercard = card; // the player has a variable called playercard cardname += card.getStr(); } So the information from the card is passed to the playercard. That works. What I can't understand is why cardname comes out as null. It doesn't make sense! The getStr method merely returns the card's string, that's all it does, and it works in the world when I run the program and inspect the card object itself. Why does it return as null in this method? I don't get it! It should be "A" or "2" or whatever, but it keeps coming up as null!
danpost danpost

2011/7/21

#
AwesomeNameGuy, did you make your 'getStr' method a 'public' method?
AwesomeNameGuy AwesomeNameGuy

2011/7/21

#
Yeah it is simply public String getStr() { return theString; }
danpost danpost

2011/7/21

#
Maybe I am a bit confused, maybe I do not have enough information to help. But I noticed in your assignCard() method above that you are changing 'playercard' to the value that is in 'card'; ?! Should they not be switched -- assigning 'card' to the value of 'playercard' (which already has a value)? Oh, I see, that was setting the value of 'playercard' What is 'theString', it that actually part of your code?
davmac davmac

2011/7/22

#
AwsomeNameGuy, I suggest you upload the scenario (with source) so that we can take a better look at it. There's nothing obviously wrong with the code you have shown above. If you do upload it, make sure to post a link to it in this discussion!
AwesomeNameGuy AwesomeNameGuy

2011/7/22

#
Thanks for the replys. I figured it out, only took 3 hours of trying different stuff and pulling my hair out, it was driving me crazy! It seemed like it should have been so simple! Yeah theString was in the code, it is assigned a value depending on what type of card it is, which is determined in a constructor. It's not the actual code word for word, I changed it to make it more obvious what it was, but it is basically the same, just names have been changed. The problem was, the card doesn't get assigned a string right away when it is made, it gets assigned a string when act() is called ( if (!stringAssigned) {assignString();} ) and assignString() just assigned a value to theString based on what type of card it was. So I was trying to get the string before the card had even acted, and theString was still null. The problem was solved by putting assignString() into the constructor where I think it probably should have been in the first place. It eluded me because when I inspected playercard the variables were what they were supposed to be, I guess I didn't know that actors that weren't part of the world acted! If that makes sense. EDIT: I might upload the source, I dunno, I just want to work on it a little while longer.
mik mik

2011/7/22

#
For the future, here's a tip how you can debug this sort of thing. The first question to ask would have been: Is the string you're getting from the card not being assigned correctly, or is it assigned correctly, but it was actually null? To investigate this: If you have the card as an actor visible in the world: Right-click on the card, select "Inspect" and look at the current value of the string field. If the card is not visible on screen (but the player is): Inspect the player in the same way; select the 'playercard' field, and inspect that in turn. Again, check the string. In general: when unexpected things happen, inspecting the state of the objects helps! Michael
You need to login to post a reply.