1

I have Got Two Array List NumINdb(VARIABLES) and common(VARIABLES)...I Have Compared Two Arraylist and Retrieved Common Elements from the Arraylist in a New Arraylist Finalarr(VARIABLE)...

I am Passing the Arraylist to a Function getPlayers(Finalarr).....Below is the Function...

I have Phone numbers in the FinalArr Arraylist and Want Their names From the Contact Book / Phone book of the phone....I want to Store the Value in a Arraylist...

 private ArrayList < DATA_CONTACT > getPlayers(ArrayList < String > filterVALUES) {
    ArrayList < DATA_CONTACT > players = new ArrayList < DATA_CONTACT > ();

    ContentResolver cr = getContext().getContentResolver();

    Cursor phoneCursor;
    for (int o = 0; o < filterVALUES.size(); o++) {

        phoneCursor = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", new String[] {
                filterVALUES.get(o).toString()
            }, null);

        DATA_CONTACT p = null;
        while (phoneCursor.moveToNext()) {

            p = new DATA_CONTACT();
            final String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contact_display_name = phoneCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // filtered.add(phoneNumber.toString());

            p.setName(contact_display_name);
            p.setPhone(phoneNumber.toString());
            p.setImg(R.drawable.com_facebook_button_like_background);
        }
        phoneCursor.close();
        players.add(p);

    }
    return players; //Returns a List of Contact Matching the Arraylist..
}

Error...

FATAL EXCEPTION: main
                                               Process: com.example.cosmic.zumi_test, PID: 534
                                               java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndex(java.lang.String)' on a null object reference
                                                   at com.example.cosmic.zumi_test.Contact_FRAGMENT3.getPlayers(Contact_FRAGMENT3.java:243)
                                                   at com.example.cosmic.zumi_test.Contact_FRAGMENT3.access$100(Contact_FRAGMENT3.java:57)
                                                   at com.example.cosmic.zumi_test.Contact_FRAGMENT3$1.onDataChange(Contact_FRAGMENT3.java:157)
                                                   at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
                                                   at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
                                                   at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
                                                   at android.os.Handler.handleCallback(Handler.java:815)
                                                   at android.os.Handler.dispatchMessage(Handler.java:104)
                                                   at android.os.Looper.loop(Looper.java:194)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5643)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
Akshay
  • 19
  • 4
  • I dont understand what it is you want from us? That we write code for you? Meaning: please do not only talk about what you **want** to do; but explain what **prevents** you getting there. Besides: you want us to spend our time to help you; so you please take the 1 min it takes to properly format/indent your source code. Long story short: spent some time here [help] and read what/how to ask here. – GhostCat Jan 05 '17 at 08:40
  • @ghostCAT I have Put my Error Message..I am Getting..Pls Help... – Akshay Jan 05 '17 at 08:41
  • That makes more sense (still: why the lazy formatting for the stack trace) ... anyway: start reading here: http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – GhostCat Jan 05 '17 at 08:41
  • I want Help with my Query i have Passed...Pls Check... VARIABLE contact_display_name is giving Null Error... – Akshay Jan 05 '17 at 08:43
  • Dont just talk about what you *want*. Take the time to digest the feedback you already got; especially that link in my **second** comment; besides: you got an answer already. – GhostCat Jan 05 '17 at 08:46
  • 1
    i totally fantasise about lower-casing this entire question... – marmor Jan 05 '17 at 08:47
  • @marmor Collecting 105 rep during two hours of being online makes me smile. Reading your comment made me laugh! I hope I just made you smile too by improving your question track record ;-) – GhostCat Jan 05 '17 at 09:04

2 Answers2

1

The exception points (most likely?) here:

cursor.getColumnIndex(...

and tells you that cursor is null.

Did you mean phoneCursor instead?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

If you just want to get only names from array list(I am assuming your array list contains numbers) then below is the method that returns name against contact number.

public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return contactName;
}

Don't forget to add 'read contact' permission in manifest

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58