This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Serializable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serializable problem" Watch "Serializable problem" New topic
Author

Serializable problem

Michael Duff
Greenhorn

Joined: Mar 13, 2010
Posts: 18
Hi,

I need some help with Serializable, I can't seem to get it to work properly! I want to save my HashMap and be able to load it, but so far without success. By the looks of the code it seems correct, maybe the problem could be related to the HashMap which I want to save/load? I have implemented and imported Serializable for the class.


I access these methods from an ActionListener method, I have bound them to a button in a menu.

Thanks for your time,

Michael
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Looks OK to me. You haven't told us what the actual problem is. What do you expect to have happen, and what happens instead? If there's an exception stack trace, please copy and paste it into your reply.


[Jess in Action][AskingGoodQuestions]
Michael Duff
Greenhorn

Joined: Mar 13, 2010
Posts: 18
I got a HashMap where I put my keys and values in, but whenever I save and try to load it next time I start the program the "information" in the HashMap seems to be gone. I do get the print confirmation I had in the code (which says I have saved or loaded the file), no errors, which makes me think I am doing something wrong with my HashMap.

I am using Swing where I have buttons assigned for the save and load methods, and another button with which I am adding keys and values into HashMap. I have the keys from the HashMap in a JComboBox, which I would like to be able to save and load if I wanted to.

Cheers,

Michael
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Maybe the actual HashMap isn't in scope when you save it, or maybe there is more than one "lang" variable. These things happen!

Try adding a line like

System.out.println("Saving" + lang)

right before the call to writeObject(), and another one like

System.out.println("Loaded: " + lang)

right after readObject() -- you might be surprised at the results.
Michael Duff
Greenhorn

Joined: Mar 13, 2010
Posts: 18
Good one, now I know it does in fact save and load the HashMap. The problem must be related to JComboBox, which doesn't seem to save/load the values I have added to it.

Edit. I found the solution! In my ClickListener method where I load the HashMap I had to do a for each loop over the keys so it would actually repopulate the JComboBox.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serializable problem
 
Similar Threads
Serialization is not for staitics
Inner class & serialize
Can we Serialize a HashMap object in a Thread
about object serialization
serialized ArrayList object