I'm writing a function that will add Android contacts stored in my database to the user's contact list. I asked a similar question yesterday, Programmatically Inserting New Android Contacts into Phone, where I just asked for an example of inserting an Android contact to the contacts list. While I received a correct answer, it didn't solve the problem I was having. What my app does is take a contact that the user stored in my database, turns it into a contact object, and then takes information from the contact object and inserts it back into the phone. My issue is that the app crashes the second I try to insert any data to the device. Here's the code that I use up to the point of the crash:
public void addContact(Contact contact)
{
ContentValues values = new ContentValues();
values.put("contact_id", contact.getContactId());
values.put("lookup", contact.getLookupKey());
values.put("mimetype", StructuredName.CONTENT_ITEM_TYPE);
values.put("data1", contact.getStructuredName().getDisplayName());
values.put("data2", contact.getStructuredName().getGivenName());
values.put("data3", contact.getStructuredName().getFamilyName());
values.put("data4", contact.getStructuredName().getPrefix());
values.put("data5", contact.getStructuredName().getMiddleName());
values.put("data6", contact.getStructuredName().getSuffix());
context.getContentResolver().insert(Data.CONTENT_URI, values);
values.clear();
}
I don't see any reason that wouldn't work, after all, I have the permissions to read and write contacts in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
Anyway, the person who responded to my last question provided the code,
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, 001);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "Nirav");
Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
Running that, I still get the crash. Here's the logcat for that code the second I click the "Restore Contact" button in my app:
05-08 05:57:40.905: D/AndroidRuntime(2917): Shutting down VM
05-08 05:57:40.905: W/dalvikvm(2917): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-08 05:57:40.945: E/AndroidRuntime(2917): FATAL EXCEPTION: main
05-08 05:57:40.945: E/AndroidRuntime(2917): java.lang.NullPointerException
05-08 05:57:40.945: E/AndroidRuntime(2917): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
05-08 05:57:40.945: E/AndroidRuntime(2917): at com.protextyou.contacts.ContactHandler.addContact(ContactHandler.java:46)
05-08 05:57:40.945: E/AndroidRuntime(2917): at com.protextyou.StartPage$12.onClick(StartPage.java:542)
05-08 05:57:40.945: E/AndroidRuntime(2917): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-08 05:57:40.945: E/AndroidRuntime(2917): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 05:57:40.945: E/AndroidRuntime(2917): at android.os.Looper.loop(Looper.java:137)
05-08 05:57:40.945: E/AndroidRuntime(2917): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 05:57:40.945: E/AndroidRuntime(2917): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 05:57:40.945: E/AndroidRuntime(2917): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 05:57:40.945: E/AndroidRuntime(2917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 05:57:40.945: E/AndroidRuntime(2917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 05:57:40.945: E/AndroidRuntime(2917): at dalvik.system.NativeStart.main(Native Method)
And of course, when I add a try/catch statement to the code, it comes up with "Exception: Null". So, I am guessing that NullPointerException is my issue, but I don't know what could be causing it. Could somebody explain what I might need to fix in order to make my app stop crashing?