I am new to android app development and I tried to write an arrayList to a file, but I've been getting problems when attempting to do that in my code:
public class AddActivity extends Activity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public transient Context ctx;
private final String filename = "Accounts";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
final EditText name = (EditText) findViewById(R.id.name);
final EditText username = (EditText) findViewById(R.id.username);
final EditText password = (EditText) findViewById(R.id.password);
final EditText type = (EditText) findViewById(R.id.type);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("unchecked")
public void onClick(View arg0) {
String n = name.getText().toString();
String u = username.getText().toString();
String p = password.getText().toString();
String t = type.getText().toString();
Account newAccount = new Account (n, u , p, t);
ArrayList<Account> accounts;
String filePath = getFilesDir().getPath().toString() + "/" + filename;
File fl = new File(filePath);
try {
if (fl.exists()){
FileInputStream fis = ctx.openFileInput(filename);
ObjectInputStream oi = new ObjectInputStream(fis);
accounts = (ArrayList<Account>) oi.readObject();
accounts.add(newAccount);
oi.close();
fis.close();
fl.delete();
}else{
accounts = new ArrayList<Account>();
accounts.add(newAccount);
}
fl.createNewFile();
FileOutputStream fos = (openFileOutput(filename, Context.MODE_PRIVATE));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(accounts);
oos.flush();
oos.close();
fos.close();
} catch (IOException e) {
Log.e("InternalStorage", e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
My logCat says this:
05-20 18:56:23.782: E/AndroidRuntime(2242): FATAL EXCEPTION: main
05-20 18:56:23.782: E/AndroidRuntime(2242): Process: com.chenw15.accountmanager, PID: 2242
05-20 18:56:23.782: E/AndroidRuntime(2242): java.lang.NullPointerException
05-20 18:56:23.782: E/AndroidRuntime(2242): at com.chenw15.accountmanager.AddActivity$1.onClick(AddActivity.java:56)
05-20 18:56:23.782: E/AndroidRuntime(2242): at android.view.View.performClick(View.java:4438)
05-20 18:56:23.782: E/AndroidRuntime(2242): at android.view.View$PerformClick.run(View.java:18422)
05-20 18:56:23.782: E/AndroidRuntime(2242): at android.os.Handler.handleCallback(Handler.java:733)
05-20 18:56:23.782: E/AndroidRuntime(2242): at android.os.Handler.dispatchMessage(Handler.java:95)
05-20 18:56:23.782: E/AndroidRuntime(2242): at android.os.Looper.loop(Looper.java:136)
05-20 18:56:23.782: E/AndroidRuntime(2242): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-20 18:56:23.782: E/AndroidRuntime(2242): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 18:56:23.782: E/AndroidRuntime(2242): at java.lang.reflect.Method.invoke(Method.java:515)
05-20 18:56:23.782: E/AndroidRuntime(2242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-20 18:56:23.782: E/AndroidRuntime(2242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-20 18:56:23.782: E/AndroidRuntime(2242): at dalvik.system.NativeStart.main(Native Method)
Every time I click my add button, I get an error that says "Unfortunately, (App name) has stopped". Please help. Thanks.