sreejith v s sreejith v s -4 years ago 112
Java Question

Unable to Serialize Hashmap ,java.io.NotSerializableException

public class Common implements Serializable{

private static HashMap<Integer,List<LevelList>> levelListMap = new HashMap<>();

public static Map<Integer, List<LevelList>> getLevelListMap(Context context) {

File file = new File(context.getDir("data", MODE_PRIVATE), "map");
ObjectInputStream inputStream = null;
try {
inputStream = new ObjectInputStream(new FileInputStream(file));
levelListMap = (HashMap<Integer, List<LevelList>>) inputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return levelListMap;
} ...
}


I am unable to serialize hashmap.I keep getting
java.io.NotSerializableException
for
levelListMap = (HashMap<Integer, List<LevelList>>) inputStream.readObject();


public class LevelList implements Serializable{

public int id;
public String title;
public String imgurl;
public String songurl;
public String songtext;
boolean isFavourite;

public void release() {

}


public void setFavourite(boolean favourite) {
isFavourite = favourite;
}

public boolean isFavourite(){
return isFavourite;
}

}

Answer Source

HashMap is serializable, but the keys and values must also be serializable. Make sure that all keys and values are serializable, and that their fields are also serializable (excluding transient and static members).

Edit:

HashMap<Integer,List<LevelList>>, is your List implementation serializable?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download