-3

I am building a contacts app and have created a database for it. Each time I run it, it shows the exception below.

Sample of logcat error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
        at com.example.joey.sqlite.MainActivity.onResume(MainActivity.java:48)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
        at android.app.Activity.performResume(Activity.java:6076)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3039)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3081) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2447) 
        at android.app.ActivityThread.access$800(ActivityThread.java:156) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:211) 
        at android.app.ActivityThread.main(ActivityThread.java:5389) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

How can I resolve it?

The error is reported to be in the for loop:

@Override
protected void onResume() {

    DBHelper dbHelper = new DBHelper(this);

    namesList = new ArrayList<String>();
    for (int i=0; i< dbHelper.getAllContacts().size();i++){
        namesList.add(dbHelper.getAllContacts().get(i).getName());
    }

    adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,namesList);

    list.setAdapter(adapter);
    super.onResume();
}

Sample For Database Read Method:

public ArrayList<Contact> getAllContacts() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<Contact> contactArrayList = new ArrayList<Contact>();

    Cursor cur = db.rawQuery("select * from " + TABLE,null);
    cur.moveToFirst();
    while (cur.isAfterLast() == false) {
        try {
            Contact contact_new =  new Contact();
            contact_new.setName(cur.getString(cur.getColumnIndex(NAME)));
            contact_new.setEmail(cur.getString(cur.getColumnIndex(EMAIL)));
            contact_new.setPhone(cur.getString(cur.getColumnIndex(PHONE)));

            contactArrayList.add(contact_new);
            cur.moveToNext();

            return contactArrayList;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    return null;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Joseph Joey
  • 45
  • 1
  • 10
  • as your log cat shows your arraylist is returning null , so first check if contactArrayList is not null, Secondly make contactArrayList public. – Mayank Bhatnagar Sep 16 '16 at 14:11
  • @MayankBhatnagar *your arraylist is returning null* <= what does it mean ? **arraylist may be null** or **some method of arraylist may returns null** *your arraylist is returning null* means nothing – Selvin Sep 16 '16 at 14:18
  • also your `getAllContacts` implementation makes no sens as have `return` statement inside loop – Selvin Sep 16 '16 at 14:20

1 Answers1

1

Just add this check above your for-loop to avoid a crash if your database request returns null:

if (dbHelper.getAllContacts() != null) {
    for(...) {...}
} 
Katharina
  • 1,612
  • 15
  • 27