5

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?

Pentium10
  • 204,586
  • 122
  • 423
  • 502

2 Answers2

10

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.

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
-1

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Unfortunately that is inbuilt in the Android SDK system and I have no access to those. – Pentium10 Jul 20 '10 at 08:56
  • Search your SDK for the source of this file and check the code. – Aaron Digulla Jul 20 '10 at 09:17
  • As stated in the question this happens only on the phone, I don't have access to those files on the phone. Ognian told that it might be a device specific bug. – Pentium10 Jul 20 '10 at 09:19
  • Try to open a bug report with the device manufacturer. Since the device shows line numbers, you can also try to download the class file and decompile it. – Aaron Digulla Jul 20 '10 at 12:58