1

I am doing contacts inserting android app for that using applyBatch method,My content resolver showing null pointer exception.

public void import_contacts(File paramFile, int paramInt)
            throws IOException {
        String[] arrayOfString;
        i1 = 0;
        try {
            FileInputStream localFileInputStream = new FileInputStream(
                    paramFile);
            BufferedReader localBufferedReader = new BufferedReader(
                    new InputStreamReader(localFileInputStream));
            for (;;) {
                String str = localBufferedReader.readLine();
                if (str == null) {
                    localBufferedReader.close();
                    mProgress.setProgress(100);
                    mHandler.post(new Runnable() {
                        public void run() {
                            alertdialog_Contact(totalRecToImport
                                    + " Contacts were imported Successfully");
                        }
                    });
                    return;
                }
                i1++;
                float f = i1;
                mProgressStatus = ((int) (f / (paramInt / 100)));
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgress.setProgress(mProgressStatus);
                    }
                });
                Log.d("TAG", str);
                arrayOfString = str.split(";");
                if (arrayOfString.length >= 4) {
                    // if (arrayOfString.length != 4) {
                    // break;
                    // }
//                  insertLine(arrayOfString[0], "", arrayOfString[3], "", "",
//                          "", "", "", "", "", "", "");
                    op_list = new ArrayList<ContentProviderOperation>();

                    op_list.add(ContentProviderOperation
                            .newInsert(RawContacts.CONTENT_URI)
                            .withValue(RawContacts.ACCOUNT_TYPE, null)
                            .withValue(RawContacts.ACCOUNT_NAME, null).build());
                    op_list.add(ContentProviderOperation
                            .newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValue(
                                    ContactsContract.Data.MIMETYPE,
                                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                            .withValue(
                                    ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                                    arrayOfString[0]).build());

                    op_list.add(ContentProviderOperation
                            .newInsert(Data.CONTENT_URI)
                            .withValue(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                            .withValue(Phone.NUMBER, arrayOfString[3])
                            .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
                            .withValue(Phone.TYPE, Phone.TYPE_WORK).build());
                    applytobatch++;
                }
                if(applytobatch == 100){
                    applytobatch=0;
                    try {
                           getContentResolver().
                              applyBatch(ContactsContract.AUTHORITY, op_list);
                        } catch (RemoteException e) {
                        } catch (OperationApplicationException e) {
                        }
                }
            }
        } catch (FileNotFoundException localFileNotFoundException) {
            for (;;) {
                Toast.makeText(this, localFileNotFoundException.getMessage(), 0)
                        .show();
                localFileNotFoundException.printStackTrace();
            }
        }
    }

Error:

12-20 23:59:21.268: E/AndroidRuntime(3151): FATAL EXCEPTION: Thread-18
12-20 23:59:21.268: E/AndroidRuntime(3151): java.lang.NullPointerException
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.os.Parcel.readException(Parcel.java:1328)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:137)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:449)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at android.content.ContentResolver.applyBatch(ContentResolver.java:639)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at com.example.contact_export.MainActivity.import_contacts(MainActivity.java:385)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at com.example.contact_export.MainActivity$4.run(MainActivity.java:294)
12-20 23:59:21.268: E/AndroidRuntime(3151):     at java.lang.Thread.run(Thread.java:1019)

I am trying to insert 10000 contacts,please tell which method i have to follow?

Boopathi
  • 281
  • 4
  • 11

1 Answers1

0

You can't insert contacts with null account name and account type. Also keep batch size to maximum of 50. The account name and type should be a writable account, or you should create your own account in the device.

Ankur Kumar
  • 972
  • 7
  • 12