Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By doWhile

Thread: problem with loading/saving hashmaps

  1. #1
    S.Mason is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default 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);
    This is the actual "save" method:
    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.";
            }  
        }
    that part seems to work without errors. The problem comes when I try to load it.

    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.");
                }
    here is that loadFiles method:

    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);
            }   
        }
    here is the actual load method that's been called at the end:
    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;
        }
    and finally here is the error I'm getting everytime the program tries to load that hashmap

    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)
    As far as I can tell it seems like I'm not doing the class cast right. this part though
    Java Code:
     java.lang.B
    oolean cannot be cast to java.awt.Font
    keeps changing. Sometimes it says number instead of font or color. I'm thinking that the program is not saving the hashmap properly in the first place? Any thoughts on this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    S.Mason is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: problem with loading/saving hashmaps

    Quote Originally Posted by Fubarable View Post
    What are you saving in your HashMap?
    Java Code:
    (HashMap<Byte,HashMap<String,ArrayList<String>>>)

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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
    Fubarable and DarrylBurke like this.

Similar Threads

  1. saving / loading a file - TicTacToe
    By Herah in forum New To Java
    Replies: 0
    Last Post: 12-01-2011, 03:59 AM
  2. Problem with saving from JFileChooser
    By fireshadow4126 in forum AWT / Swing
    Replies: 2
    Last Post: 11-24-2010, 09:21 PM
  3. Saving from, and loading to an array.
    By VisionIncision in forum New To Java
    Replies: 4
    Last Post: 11-23-2010, 10:47 AM
  4. Saving and loading information
    By applewood13 in forum New To Java
    Replies: 1
    Last Post: 09-30-2010, 06:29 AM
  5. Replies: 0
    Last Post: 08-02-2010, 11:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •