Question

I have an Activity from which I am trying to save the contact using data provided by the user.I find that the Uri returned is null...I have tested some of the code without allowing null values elsewhere and it works...but here it does not seem to be working.Instead it gives me a NullPointerException.

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.blutechnologies.scancard.BusinessCard$2.onClick(BusinessCard.java:195)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

This is the code I am using to save the contact using the Android ContactsContract API by applying a batch operation using ContentProviderOperation,the ContentProviderResult and the applyBatch method.I aim to return a Uri which will then be used to View the Contact Card using an Intent with ACTION_VIEW.

                btn_save.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View view) {

                    Uri uri=saveContacts(sname.getText().toString(), sphone.getText().toString(), sphone2.getText().toString(), semail.getText().toString(), semail2.getText().toString(), sweb.getText().toString(), sweb2.getText().toString(), pathName);
                    Log.d(TAG, "Contact saved with uri "+uri.toString());
                    Intent intent=new Intent(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    startActivity(intent);
                }});
}   

public Uri saveContacts(String name,String mobile,String home_phone,String work_email,String personal_email,String website,String website2,String pathName)
{
    ContentProviderOperation operation=null;
    ContentProviderResult[] res = null;
    ArrayList<ContentProviderOperation> ops=new ArrayList<ContentProviderOperation>();
    Uri uri=null;
    int rawContactId=ops.size();    
    operation=ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build();
    ops.add(operation);

    if(name!=null)
    {
        operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE).withValue(StructuredName.DISPLAY_NAME,name).build();
        ops.add(operation);
    }
    if(mobile!=null)
    {   
        operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER,mobile).withValue(Phone.TYPE,Phone.TYPE_MOBILE).build();
        ops.add(operation);
    }
    if(home_phone!=null)
    {
        operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER, home_phone).withValue(Phone.TYPE, Phone.TYPE_HOME).build();
        ops.add(operation);
    }
    if(work_email!=null)
    {   
        operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId).withValue(ContactsContract.Data.MIMETYPE,Email.CONTENT_ITEM_TYPE).withValue(Email.ADDRESS,work_email).withValue(Email.TYPE,Email.TYPE_WORK).build();
        ops.add(operation);
    }
    if(personal_email!=null)
    {   
        operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE,Email.CONTENT_ITEM_TYPE).withValue(Email.ADDRESS,personal_email).withValue(Email.TYPE,Email.TYPE_HOME).build();
        ops.add(operation);
    }
    if(website!=null)
    {   
        operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE,Website.CONTENT_ITEM_TYPE).withValue(Website.URL,website).withValue(Website.TYPE,Website.TYPE_WORK).build();
        ops.add(operation);
    }
    if(website2!=null)
    {   
    operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE,Website.CONTENT_ITEM_TYPE).withValue(Website.URL,website2).withValue(Website.TYPE,Website.TYPE_HOME).build();
    ops.add(operation);
    }
    if(pathName!=null)
    {
        byte[] imgArray=pathName.getBytes();//Error is here...found it.
        operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE).withValue(Photo.PHOTO,imgArray).build();
        ops.add(operation);
    }
    try {
        res=getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
        if(res!=null && res.length>=1)
        {
            uri=res[0].uri;
            Log.d(TAG,"Contact saved successfully");
        }

    } catch (RemoteException e) {
        Log.d(TAG,"Error performing batch operation to insert contact RemoteException",e);
    } catch (OperationApplicationException e) {
        Log.d(TAG,"Error performing batch operations to insert contact OperationApplicationException",e);
    }
    return uri;
} 
Was it helpful?

Solution

I caught the error,the code somehow mutated to a point where it was obtaining the byte[] required for saving image to the ContactsContract.Data table using:

byte[] imgArray=pathName.getAllBytes()

I changed the code to say:

   FileInputStream fos=null;
        try {
            File file=new File(pathName);
            byte[] buffer=new byte[(int)file.length()];
            fos=new FileInputStream(file);
            if(fos.read(buffer)!=-1)
            {
                Log.d(TAG, "File has been read to byte buffer");
                operation=ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactId).withValue(ContactsContract.Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE).withValue(Photo.PHOTO,buffer).build();
                ops.add(operation);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log.e(TAG,"File could not found",e);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e(TAG, "Error when performing IO operation",e);
        }
        finally
        {
            if(fos!=null)
            {
                try {
                    fos.close();
                } catch (IOException e) {
                    Log.e(TAG, "Error closing the inputStream while cleaning up");
                }
            }
        }    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top