• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting null when writing to file.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my name "matthew" is what what i type.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic