This Error Message pops when i press the button to open an other activity that has 2 text fields and a save and cancel button.The error is being pointed out to this part of the code in the save function-
listView.setAdapter(adapter);
Full Error
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
Java for the Save function
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Button btnSave = (Button) findViewById(R.id.btnSave);
ListView listView = (ListView) findViewById(R.id.listDisp);
arrayList = new ArrayList<>();
edName = (EditText) findViewById(R.id.editName);
edMobile = (EditText) findViewById(R.id.editMobile);
adapter = new ArrayAdapter<Classes>(this, R.layout.activity_add, R.id.txtItem, arrayList);
Log.i("test", " " + arrayList.size());
listView.setAdapter(adapter);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Classes class1 = new Classes(edName.getText().toString(), edMobile.getText().toString());
arrayList.add(class1);
adapter.notifyDataSetChanged();
Log.i("test", " " + arrayList.size());
try {
FileOutputStream fos = openFileOutput("Saves.tim", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(arrayList);
oos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
Java for the Load function
private ViewPager viewPager;
private SectionsPagerAdapter mSectionsPagerAdapter;
ListView lv;
public static ArrayList<Classes> arrayList;
private ArrayAdapter<Classes> adapter;
private EditText etName, etMobile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listDisp);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etName = (EditText) findViewById(R.id.editName);
etMobile = (EditText) findViewById(R.id.editMobile);
Button btSave = (Button) findViewById(R.id.btnSave);
arrayList = new ArrayList<>();
try {
FileInputStream fis = this.openFileInput("Saves.tim");
ObjectInputStream ois = new ObjectInputStream(fis);
arrayList = (ArrayList<Classes>) ois.readObject();
ois.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch(ClassNotFoundException ex){
ex.printStackTrace();
}
Method that launches the activity
public void loadAddActivity (View v)
{
startActivity(new Intent(this,AddActivity.class));
}