【发布时间】:2018-08-28 10:14:14
【问题描述】:
有些事情真的搞砸了。我在资产文件夹中有一个“.ser”文档,其中存储了 Objetcs 的 ArrayList。在一个 android 应用程序中,我想读取这些对象。有很多与此问题相关的帖子,但是没有一个可以解决我的问题。奇怪的是,当我在非 android 上下文/“普通”java 中使用类似代码时,它可以正常工作。在这里,最后一行抛出 NullPointerException - 出了什么问题?
public void getData() {
ArrayList<MyClass> output= null;
InputStream is = null;
ObjectInputStream ois = null;
try{
is = this.getAssets().open("data.ser");
ois = new ObjectInputStream(is);
output = (ArrayList<MyClass>)ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("TAG", output.get(0).getId());
}
【问题讨论】:
-
是用ObjectOutputStream写的.ser文件吗?
-
@NarayanaReddy 是的。
-
最后一行的空指针可能是因为输出数组列表的大小可能为零,或者数组列表本身为空。是哪一个?
标签: java android objectinputstream