【发布时间】:2015-08-27 17:02:02
【问题描述】:
我正在尝试将字符串添加到先前从内部存储文件中提取的 ArrayList。但是,在这一行会抛出 NullPointerException。
ArrayList<String> names = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
createLayout();
next.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("unchecked")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tripName = tripNameET.getText().toString(); //tripNameET is the EditText that provides the String to be put in the ArrayList
...
FileInputStream fis1 = null;
ObjectInputStream ois1 = null;
try {
fis1 = openFileInput("names");
ois1 = new ObjectInputStream(fis1);
names = (ArrayList<String>) ois1.readObject();
ois1.close();
fis1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
names.add(tripName); //NPE is Thrown Here
...
}
}
【问题讨论】:
-
这只是表示names为null,所以ois1.readObject()返回null。
标签: java android arraylist nullpointerexception