• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serializable problem

 
Michael Duff
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Duff
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic