2016-09-16 108 views
-3

我在構建聯繫人應用程序併爲其創建了數據庫。每次運行它時,都會顯示下面的例外情況。從數據庫讀取時出現NullPointerException

樣品logcat的錯誤:

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)  

我該如何解決呢?

報告錯誤,是在for循環:

@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(); 
} 

樣本數據庫讀取方法:

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; 
} 
+0

作爲您的日誌貓顯示您的arraylist返回null,因此首先檢查contactArrayList是否不是null,其次使contactArrayList爲public。 –

+0

@MayankBhatnagar *你的數組列表返回null * <=這是什麼意思? ** arraylist可能爲null **或** arraylist的某些方法可能會返回null ** *您的arraylist返回null *意味着什麼也沒有 – Selvin

+0

您的'getAllContacts'實現也沒什麼感覺,因爲循環內有'return'語句 – Selvin

回答

1

只需添加上述這個檢查你的for循環,以避免崩潰,如果您數據庫請求返回null:

if (dbHelper.getAllContacts() != null) { 
    for(...) {...} 
} 
if (dbHelper.getAllContacts() != null) { 
    for(...) {...} 
} 
+0

謝謝。我已經嘗試了所有建議,但它似乎沒有工作。任何進一步的幫助將不勝感激。 –

相關問題