MEAM
MEAM

Reputation: 61

NotSerializableException wiht HashMap serialize

I have a problem with serialize...I test this methods separate from class and worked correctly! But when I use this class get NotSerializableException

Code:
Property that must to be serialize:
public ArrayList<User> users;
This property is in UserManage class

This method for reading from database file:

public void readFromDataBaseFile(){
    File database=new File(this.saveUsersObject+"\\"+"Database.txt");
    if(!database.exists()){
        System.out.println("Exception:FileNotExist");
    }
    else{
        HashMap<String,Object> Data=(HashMap<String,Object>)FileHandler.readObjectInFile(database);
        this.users= (ArrayList) Data.get("users");
    }
}

And this method for writing to database file:

public void writeToDataBaseFile(){
    File database=new File(this.saveUsersObject+"\\"+"Database.txt");
    if(!database.exists()) {
        File parent=database.getParentFile();
        parent.mkdirs();
    }
    HashMap<String,Object> Data=allOfUsersToHashMap();
    if(Data.isEmpty()){
        System.out.println("DataBase HashMap Is Empty!");

    }
    else{
        FileHandler.writeObjectInFile(Data,database);
    }
}

Above method are in class UserManage.

below method is a helper method to writing an object in a file:

  1. boolean writeObjectInFile(Object obj,File file):
public static boolean writeObjectInFile(Object obj,File file){
    String Path=file.getAbsolutePath();
    if(file.exists()){
        System.out.println("Object File Was Deleted!!");
        file.delete();
    }
    else{
        try
        {

            FileOutputStream fileOut = new FileOutputStream(Path);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(obj);
            out.close();
            fileOut.close();
            System.out.printf("Serialized data is saved To :"+file.getPath());
            return true;
        }catch(IOException i)
        {
            i.printStackTrace();
        }
    }

    return false;
}

And below method is a helper method to reading an object from a file:

  1. Object readObjectInFile(File file):
 public static Object readObjectInFile(File file) {
    Object obj=null;
    if(file.isFile()){
        try
        {

            FileInputStream fileIn = new FileInputStream(file);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            System.out.println(file.getAbsoluteFile());
            obj=in.readObject();
            fileIn.close();
            in.close();
            System.out.printf("DeSerialized data is Done From:"+file.getPath());
        }catch(IOException i)
        {
            i.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    else {
        System.out.println("returned null :\\");
        System.out.println("Enter Address Of A File Not Folder Or Another! :\\");
    }
    return obj;
}

method allOfUsersToHashMap() in class UserManage:

public HashMap<String,Object> allOfUsersToHashMap(){
    HashMap<String,Object> Userdatabase=new HashMap<>();
    Userdatabase.put("users",this.users);
    return Userdatabase;
}

I must say when ArrayList<User> user is null this method worked correctly but when has user dont work!

Upvotes: 0

Views: 255

Answers (2)

juanitodread
juanitodread

Reputation: 156

Does your User POJO meets all the requisites to be serialized?

Check this previous question: What is object serialization?

Upvotes: 1

upcrob
upcrob

Reputation: 165

If the User class doesn't implement the Serializable interface, the ArrayList won't be able to be serialized. See the JavaDoc for Serializable for implementation details: https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html

Upvotes: 5

Related Questions