Search code examples
javanullhashmapfileinputstreamfileoutputstream

java HashMap to file and file to HashMap outputs null


Hey guys I'm pretty new to developping and at this forum and wanted to create a login mirror. I had to use BlueJ because of my teacher and yeah,.. that's propably not one of the best ways to learn writing I guess (the version we have to use in school not even shows misstakes before compiling) (I personally started writing with Eclipse).

My problem is that the output I get from my hasMapReader just is null, after I "saved" the HashMap in a new file and "wrote" it in an other HashMap.

HashMap<String, Serializable> userAcc = new HashMap<String, Serializable>();
userAcc.put(name, value);

otherClass.hashMapSaver(userAcc, hashMapFile = new java.io.File(location));



    public HashMap<String, Serializable> hashMapSaver(HashMap<String, Serializable> map, java.io.File file){
            try{   
    FileOutputStream f = new FileOutputStream(file);  
    ObjectOutputStream s = new ObjectOutputStream(f);          
    s.writeObject(map);

    s.close();
    f.close();

    }catch (IOException e) { 
       e.printStackTrace(); 
    }
    return map;
    }




   HashMap<String, Serializable> userAcc2 = new HashMap<String, Serializable>();
   otherClass.hashMapReader(userAcc2, new java.io.File(hashMapFile));

(userAcc2 is in the third "main" class) then the hashMapReader in the other class:

 public HashMap<String, Serializable> hashMapReader(HashMap<String, Serializable> map2, java.io.File file){

    try{   
    FileInputStream f = new FileInputStream(file);  
    ObjectInputStream s = new ObjectInputStream(f);          
    map2 = (HashMap<String, Serializable>) s.readObject();
    f.close();
    s.close();


    }catch (IOException e) { 
       e.printStackTrace(); 
       return null;
    }  catch (ClassNotFoundException c) {
     c.printStackTrace();
     return null;
    }
    return map2;
}



System.out.println("userAcc2: " + userAcc2.get(name));
System.out.println("HashMap Keys: " + userAcc2.keySet());

but console just prints out:

userAcc2: null; HashMap Keys: []

An other problem I think, is that the file may gets replaced every time I start this program.

I'm trying to solve this nearly a week for now.

Thanks :),


Solution

  • You map2 is an argument in hashMapReader yet, within that method you perform the following assignment:

    map2 = (HashMap<String, Serializable>) s.readObject();
    

    That that updates the map2 variable, but not the object referenced by map2. Since hashMapReader returns map2, the following change would fix your problem:

    userAcc2 = otherClass.hashMapReader(userAcc2, new java.io.File(hashMapFile));
    

    As a side note, the map2 argument is entirely unnecessary since you are not using object being passed anyway. Something slightly cleaner would be:

    public HashMap<String, Serializable> hashMapReader(java.io.File file){
        HashMap<String, Serializable> map2 = null;
        try{   
            FileInputStream f = new FileInputStream(file);  
            ObjectInputStream s = new ObjectInputStream(f);          
            map2 = (HashMap<String, Serializable>) s.readObject();
            f.close();
            s.close();
        } catch (IOException e) { 
            e.printStackTrace(); 
            return null;
        } catch (ClassNotFoundException c) {
            c.printStackTrace();
            return null;
        }
        return map2;
    }
    
    ...
    userAcc2 = otherClass.hashMapReader(new java.io.File(hashMapFile));