0

I'm trying to save a map into a file but I'm getting java.io.NotSerializableException. I know this means that I have to implement Serializable to the class. The problem is that It's throwing the error even with Serializable implemented.

Code for storing:

private void storePoints(Map<String,WifiPoint> list) throws IOException{
    // store in file
    FileOutputStream fos = context.openFileOutput("points", Context.MODE_PRIVATE);
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeObject(list);
    os.close();
}

Wifipoint:

public class WifiPoint implements Serializable{
    private static final long serialVersionUID = 2;
    public String ssid;
    public String bssid;
    public String capabilities;
    public double latitude;
    public double longitude;
    public int level;
}

UPDATE: The real fix was: I needed to declare the WifiPoint in a separate file insted of declaring it inside another class.

Sorry, I didn't put the codes properly for anybody to get the correct answer

thedjaney
  • 1,126
  • 2
  • 10
  • 28

5 Answers5

2

You're trying to serialize the Map check that the Map class you are using is Serializable

Note: A HashMap is Serializable, so switch to that if possible.

cowls
  • 24,013
  • 8
  • 48
  • 78
  • this fixed it. I didn't know that HashMap was an implementation of a Map and Map itself is not Serializable – thedjaney Feb 21 '13 at 14:15
1
  • check that you actually implement java.io.Serializable
  • check that map you want to serialize also implements java.io.Serializable
WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
1

I needed to declare the WifiPoint in a separate file insted of declaring it inside another class.

thedjaney
  • 1,126
  • 2
  • 10
  • 28
0

You are passing Map object to storePoints method check whether the Map object you are passing implements Serializable. ( If you are using HashMap, TreeMap of java.util package will definitely implement Serializable. )

0

Check that Map doesn't implement the Serialization interface :

All Known Implementing Classes:
AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap,
EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons,             Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap,
UIDefaults, WeakHashMap

Source : http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

you can try this : Java: Writting/Reading a Map from disk

Community
  • 1
  • 1
Arpit
  • 12,767
  • 3
  • 27
  • 40