0

I am using Contact picker library for selecting multiple contacts but if a contact doesn't contain any number and if it is selected then it is showing some null pointer exception in the edit text field. How to remove that message and also how to remove trailing comma. Below is my Code.

 try {
            int pos = 0;
            for (Contact contact : contacts) {
                String displayName = contact.getDisplayName();
                result.append(displayName + ",");
                result.setSpan(new BulletSpan(15), pos, pos + displayName.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                //pos += displayName.length() + 1;
            }
        }
        catch (Exception e) {
            result.append(e.getMessage());
        }

        contactsView.setText(result);
sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • which library you're using? does Contact class has any method to getPhoneNumber ? – Pr38y Jun 22 '16 at 12:18
  • com.onegravity.contactpicker this library........ContactPickerActivity is the class and yes it has a method to getPhoneNumber – stackover65 Jun 22 '16 at 12:42
  • then check if condition to check phone number is there or not, based on that update `result` – Pr38y Jun 22 '16 at 12:46

1 Answers1

0

please try to check this code

 void getAllContacts() {
    ArrayList<String> nameList = new ArrayList<>();
    ArrayList<String> numberList = new ArrayList<>();

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
    String[] list = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.Contacts._ID};
    Cursor cursor = getContentResolver().query(uri, list, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

    cursor.moveToFirst();
    if (cursor.moveToFirst()) {
        do {
            String contactNumber =     cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));


            contactNuber.add(contactNumber);
            contactsName.add(contactName);
            nameList.add(contactName);
            numberList.add(contactNumber);

        } while (cursor.moveToNext());
        cursor.close();
        myContacts.put("name", nameList);
        myContacts.put("number", numberList);


    }

}
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44