我正在尝试制作一个程序,将字符串列表保存到文件中,然后将它们读入arraylist。这是我当前的代码。
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream("friends.txt"));
} catch (FileNotFoundException e) {
File f = new File("friends.txt");
try {
f.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String obj;
try {
while ((obj = (String)input.readObject()) != null) {
friendly.add(obj);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这将返回一个空指针异常。我不太确定这里出了什么问题。