2012-07-20 52 views
0

我是将字符串的数组列表保存到本地数据库的1列中。我遇到了一些问题。谁能告诉我,我错了......Serialise/Deserialise列表<String>

private String serializeArray(List<String> array) { 
    try { 
     ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); 
     ObjectOutputStream oos = new ObjectOutputStream(bytesOut); 
     oos.writeObject(array); 
     oos.flush(); 
     oos.close(); 
     return Base64.encodeToString(bytesOut.toByteArray(), Base64.NO_WRAP); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

private ArrayList<String> deserializeArray(String string) { 
    Log.d("USERDAO", string); 
    try { 
     ByteArrayInputStream bytesIn = new ByteArrayInputStream(Base64.decode(string, Base64.NO_WRAP)); 
     ObjectInputStream ois = new ObjectInputStream(bytesIn); 
     return (ArrayList<String>) ois.readObject(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

返回deserialise阵列上的ArrayList中,当我得到一个空指针异常。 serialiseArray方法确实返回一个字符串,但我不确定它是否正确。

+2

也许你可以知道空指针异常发生在哪里? :) – Edmondo1984 2012-07-20 08:40:06

+1

是不是你得到任何异常之前NullPointer异常 – 2012-07-20 08:40:59

+0

张贴异常日志 – 2012-07-20 08:41:54

回答

0

当我在Eclipse中运行这个我在这行获得java.lang.ClassCastException:

return (ArrayList<String>) ois.readObject(); 

的readObject()方法试图返回数组$ ArrayList的(无论是)和你的演员阵容导致它破裂。如果您更改了演员阵列并将反序列化的返回类型更改为List,那么您会发现这一切都可行。