Results 1 to 4 of 4
- 07-08-2012, 02:05 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
problem with loading/saving hashmaps
So normally, using hashmaps as small databases to carry some data has worked for me just fine. But this time I'm getting an error which I'm not so sure about.
When I save the hashmap, this is the code that goes into it
Java Code:SaveObjects file = new SaveObjects(); String save = file.save("ns.dat", "nsDir", DatabaseManager.database);
Java Code:public String save(String filename, String directory, Object object){ try{ FileOutputStream file = new FileOutputStream(filename); try (ObjectOutputStream stream = new ObjectOutputStream(file)) { stream.writeObject(object); } return "saved"; } catch(Exception ex){ Logger.getLogger(SaveObjects.class.getName()).log(Level.SEVERE, "save file not saved",ex); return "NOT saved."; } }
This the code in my main method:
Java Code:File saveFile = new File(file + File.separator + "ns.dat"); if(saveFile.exists()){ new NameSMART().loadFiles("ns.dat",file.getName()); } else{ Logger.getLogger(NameSMART.class.getName()).log(Level.INFO,"save file not found."); }
Java Code:private void loadFiles (String filename, String directory){ SaveObjects file = new SaveObjects(); try{ if (file.load(filename, directory) != null){ DatabaseManager.database = (HashMap<Byte,HashMap<String,ArrayList<String>>>) file.load(filename, directory); Logger.getLogger(NameSMART.class.getName()).log(Level.INFO, "Loaded files successfully"); } } catch(Exception e){ Logger.getLogger(NameSMART.class.getName()).log(Level.WARNING, "Unable to load files",e); } }
Java Code:public Object load(String filename, String directory){ Object object = null; try { FileInputStream file = new FileInputStream(directory + File.separator + filename); try { ObjectInputStream stream = new ObjectInputStream(file); try { object = stream.readObject(); } catch (ClassNotFoundException ex) { Logger.getLogger(SaveObjects.class.getName()).log(Level.SEVERE, null, ex); } } catch (IOException ex) { Logger.getLogger(SaveObjects.class.getName()).log(Level.SEVERE, null, ex); } } catch (FileNotFoundException ex) { Logger.getLogger(SaveObjects.class.getName()).log(Level.SEVERE, null, ex); } return object; }
Java Code:Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.B oolean cannot be cast to java.awt.Font at javax.swing.plaf.nimbus.NimbusStyle.getFontForState(Unknown Source) at javax.swing.plaf.synth.SynthStyle.installDefaults(Unknown Source) at javax.swing.plaf.nimbus.NimbusStyle.installDefaults(Unknown Source) at javax.swing.plaf.synth.SynthViewportUI.updateStyle(Unknown Source) at javax.swing.plaf.synth.SynthViewportUI.installDefaults(Unknown Source ) at javax.swing.plaf.synth.SynthViewportUI.installUI(Unknown Source) at javax.swing.JComponent.setUI(Unknown Source) at javax.swing.JViewport.setUI(Unknown Source) at javax.swing.JViewport.updateUI(Unknown Source) at javax.swing.JViewport.<init>(Unknown Source) at javax.swing.JScrollPane.createViewport(Unknown Source) at javax.swing.JScrollPane.<init>(Unknown Source) at javax.swing.plaf.basic.BasicComboPopup.createScroller(Unknown Source) at javax.swing.plaf.basic.BasicComboPopup.<init>(Unknown Source) at javax.swing.plaf.synth.SynthComboPopup.<init>(Unknown Source) at javax.swing.plaf.synth.SynthComboBoxUI.createPopup(Unknown Source) at javax.swing.plaf.basic.BasicComboBoxUI.installUI(Unknown Source) at javax.swing.plaf.synth.SynthComboBoxUI.installUI(Unknown Source) at javax.swing.JComponent.setUI(Unknown Source) at javax.swing.JComboBox.setUI(Unknown Source) at javax.swing.JComboBox.updateUI(Unknown Source) at javax.swing.JComboBox.init(Unknown Source) at javax.swing.JComboBox.<init>(Unknown Source) at namesmart.NameSMART.initComponents(NameSMART.java:76) at namesmart.NameSMART.<init>(NameSMART.java:29) at namesmart.NameSMART$19.run(NameSMART.java:1002) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour ce) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Java Code:java.lang.B oolean cannot be cast to java.awt.Font
-
Re: problem with loading/saving hashmaps
What are you saving in your HashMap?
- 07-08-2012, 04:45 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 7
- Rep Power
- 0
- 07-08-2012, 04:53 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
Re: problem with loading/saving hashmaps
Just going by your exception alone, I have seen a similar problem using Nimbus. See:
http://bugs.sun.com/view_bug.do;jses...bug_id=6785663
The problem I experienced was most definitely a threading issue, as a thorough investigation led me to small pieces of code mistakenly updating Swing components on a thread other than the EDT. Correctly dispatching the updates to the EDT fixed the problem for me, so I'd recommend double checking your threading and how it relates to GUI updates
Similar Threads
-
saving / loading a file - TicTacToe
By Herah in forum New To JavaReplies: 0Last Post: 12-01-2011, 03:59 AM -
Problem with saving from JFileChooser
By fireshadow4126 in forum AWT / SwingReplies: 2Last Post: 11-24-2010, 09:21 PM -
Saving from, and loading to an array.
By VisionIncision in forum New To JavaReplies: 4Last Post: 11-23-2010, 10:47 AM -
Saving and loading information
By applewood13 in forum New To JavaReplies: 1Last Post: 09-30-2010, 06:29 AM -
saving and loading information of a wizard on an XML file
By bulldo in forum EclipseReplies: 0Last Post: 08-02-2010, 11:26 AM
Bookmarks