Tutorial :How to track down which field is causing NullPointerException when inserting custom field into Contacts Data table?



Question:

The full exception I get is:

07-16 19:19:17.244: ERROR/DatabaseUtils(151): java.lang.NullPointerException  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:3069)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2930)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at com.android.providers.contacts.CContactsProvider2.insertInTransaction(CContactsProvider2.java:156)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at com.android.providers.contacts.HtcContactsProvider2.insertInTransaction(HtcContactsProvider2.java:1281)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:90)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2737)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at android.content.ContentProvider$Transport.insert(ContentProvider.java:150)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:170)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at android.os.Binder.execTransact(Binder.java:287)  07-16 19:19:17.244: ERROR/DatabaseUtils(151):     at dalvik.system.NativeStart.run(Native Method)  

The code I used is:

public void saveFormality() {      ContentValues values = new ContentValues();      values.put(Data.DATA1, this.getFormality() ? "1" : "0");      saveDataWithMimeType(clsContacts.MIMETYPE_FORMALITY, values, this.getId());  }      private void saveDataWithMimeType(String mimetype, ContentValues values, String contactid) {      try {          int mod = ctx.getContentResolver().update(                  Data.CONTENT_URI,                  values,                  ContactsContract.Data.RAW_CONTACT_ID + "=" + contactid + " AND " + ContactsContract.Data.MIMETYPE + "= '"                          + mimetype + "'", null);            if (mod == 0) {              values.put(Data.RAW_CONTACT_ID, contactid);              values.put(Data.MIMETYPE, mimetype);              // this is where exception occurs              Uri u=ctx.getContentResolver().insert(Data.CONTENT_URI, values);          }      } catch (Exception e) {          e.printStackTrace();      }  }  

This happens on the phone only, and not in the emulator. All fields were checked and none of them are nulls. What could be the cause?


Solution:1

Figured it out via an enormous process of poking around in the raw data for manually added contacts, followed by trial and error to figure out what data the content provider was insisting on having. It's simple enough:

When inserting a new phone record, you must specify a Data.Type (the column named "data2"). If you don't specify this, HTC's Contacts ContentProvider will crash with a NullPointerException. The stock Android Contacts ContentProvider doesn't crash, it simply defaults to TYPE_OTHER.

Damn and blast HTC for (a) implementing a different Contacts ContentProvider that does not play by the documented rules, and (b) for only releasing the kernel source code. We need HtcContactsProvider2.java to see what their code thinks the problem is.


Solution:2

Split the line ContactsProvider2.java:3069 into several which only contain a single dot. That way, the line number will match a single field dereference which will tell you directly which field is null.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »