0

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));
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98

2 Answers2

2

Your error is occurring because listView is not initialized. I see that you attempted to initialize it. My guess is that the id R.id.listDisp either doesn't exist, or isn't a part of the R.layout.activity_add which is the layout that you inflate in this class.

TL;DR. The error is occurring because of this line:

ListView listView = (ListView) findViewById(R.id.listDisp);

listDisp isn't part of the activity_add layout

Sage Smith
  • 441
  • 3
  • 10
1

Your layout activity_add probably doesn't have a listview with id listDisp

khusrav
  • 5,267
  • 5
  • 27
  • 38