1

I'm accessing the phone numbers of all the contacts and sending it to Parse Cloud database, but I always get a NullPointerException

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.parse.ParseUser.put(java.lang.String, java.lang.Object)' on a null object reference

This is the class where I'm executing the above:

package com.weet.weet; /**
* Created by Ashish on 11/10/15.
*/
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;

import com.parse.ParseUser;

import java.util.ArrayList;


public class getContacts {
ArrayList<String> allNumbers = new ArrayList<String>();
public ArrayList ReadPhoneContacts(Context cntx) //This Context parameter is nothing but your Activity class's Context
{
    Cursor cursor = cntx.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list
    if (contactsCount > 0)
    {
        while(cursor.moveToNext())
        {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                //the below cursor will give you details for multiple contacts
                Cursor pCursor = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                        new String[]{id}, null);
                // continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list
                while (pCursor.moveToNext())
                {
                    int phoneType       = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    //String isStarred      = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
                    String phoneNo  = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    //you will get all phone numbers according to it's type as below switch case.
                    //Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want.
                    switch (phoneType)
                    {
                        case Phone.TYPE_MOBILE:
                            //Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo);
                            allNumbers.add(phoneNo);
                            break;
                        case Phone.TYPE_HOME:
                            //Log.e(contactName + ": TYPE_HOME", " " + phoneNo);
                            allNumbers.add(phoneNo);
                            break;
                        case Phone.TYPE_WORK:
                            //Log.e(contactName + ": TYPE_WORK", " " + phoneNo);
                            allNumbers.add(phoneNo);
                            break;
                        case Phone.TYPE_WORK_MOBILE:
                            //Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo);
                            allNumbers.add(phoneNo);
                            break;
                        case Phone.TYPE_OTHER:
                            //Log.e(contactName + ": TYPE_OTHER", " " + phoneNo);
                            allNumbers.add(phoneNo);
                            break;
                        default:
                            break;
                    }
                }
                pCursor.close();
            }
        }
        cursor.close();
    }
    ParseUser currentUser = ParseUser.getCurrentUser();
    currentUser.put("friends", allNumbers);

    return allNumbers;
}

}
Ashish Patil
  • 337
  • 3
  • 4
  • 15

0 Answers0