This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting null when writing to file.  RSS feed

 
matthew christensen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya fellas,

So I have some code I'm doing for practice, I'm passing a String value called "charName" into the method setCharacterName(). Then writing it to a file, but when i check the file to see if the save was successful, it says "name null". Which leads be to believe some where down the line the Value is lost, but im not sure where!









Some help woould be nice! Thank you!
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you entering on the keyboard when you are prompted?


Hunter
 
matthew christensen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my name "matthew" is what what i type.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post your character class? one of your other methods must be changing that value. Also in your debug statements does your name print out correctly?

Hunter
 
matthew christensen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:can you post your character class? one of your other methods must be changing that value. Also in your debug statements does your name print out correctly?

Hunter


Sure can!


sorry i didnt comment my code no the debug comment gives a null.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem. In save game:


you create a new character then try to retrieve that characters name. This character doesn't have a name because you just created it. You need to get the name of the character you created before inside charactersSetup.

Hunter
 
matthew christensen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:I see the problem. In save game:


you create a new character then try to retrieve that characters name. This character doesn't have a name because you just created it. You need to get the name of the character you created before inside charactersSetup.

Hunter


I dont think i quite understand could you explain a little more?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok well when you create a new character object. You basically create a blank slate, the character object doesn't have a name. So when you call the getCharacterName() method it is trying to return a name that doesn't exist and is printing out null. Does that help?

Hunter
 
matthew christensen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:Ok well when you create a new character object. You basically create a blank slate, the character object doesn't have a name. So when you call the getCharacterName() method it is trying to return a name that doesn't exist and is printing out null. Does that help?

Hunter

OK im following, So how would i access the value for name?
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
matthew christensen wrote:
Hunter McMillen wrote:Ok well when you create a new character object. You basically create a blank slate, the character object doesn't have a name. So when you call the getCharacterName() method it is trying to return a name that doesn't exist and is printing out null. Does that help?

Hunter

OK im following, So how would i access the value for name?


No, I don't think you are following. Hunter said that the value for name was null, because you hadn't assigned anything to the name yet. So your problem is not that you aren't accessing that value correctly. You are accessing it correctly. The problem is that you are trying to use the character's name before you assign a name to the character, so what you access is null.

So: either assign the character a name as soon as you create it, or change your "character" class so that the name has a default value:


Or perhaps the problem is that you shouldn't be using a new character object at that point. Can you explain why saving the game requires you to create a new character with no information?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you do this:

character charSave = new character();

a new character object is created. It's like having a person walk in the door, and you hand them a "Hi, my name is" sticker. There is no name written on that sticker yet.

then, two lines later, you say

charSave.getCharacterName()

which says "read the name off the 'Hi, my name is' sticker this person is wearing" - only there is nothing there. You need to write that name on the sticker.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're also creating a character object in your loadGame method, but then you assign it to a local variable, so after the method is complete, the character is discarded. If you need a character to be a permanent feature of your class, you should use an instance variable to keep a reference to it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!