2016-09-16 12 views
-3

私は連絡先アプリを構築しており、そのデータベースを作成しています。私がそれを実行するたびに、それは以下の例外を示しています。 logcatエラーのデータベースからの読み取り時にNullPointerExceptionが発生する

サンプル:

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ループの上には、このチェックを追加

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

あなたのログcatにarraylistがnullを返していることが示されているので、最初にcontactArrayListがnull、次にcontactArrayListをpublicにします。 –

+0

@MayankBhatnagar *あなたのarraylistがnullを返す* <=それはどういう意味ですか? ** arraylistはnull **または** arraylistのいくつかのメソッドはnullを返す可能性があります** * arraylistがnullを返す*は何も意味しません – Selvin

+0

また、 'getAllContacts'実装はループ内で' return'ステートメントを持つように感覚しません – Selvin

答えて

1

:データベース読む方法については

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

サンプルデータベースリクエストはnullを返します。null:

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

ありがとう。アイブはすべての提案を試みましたが、それは動作していないようです。それ以上の助けに感謝します。 –

関連する問題