Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:03:10+00:00 2026-05-27T07:03:10+00:00

In my Android, I am reading the contacts by using content resolver.But content resolver

  • 0

In my Android, I am reading the contacts by using content resolver.But content resolver is not getting initialized ,it gives NullPointerException.
I want to use the string arrays in another class and to get values in them I have to query the database and read contacts from phone,but stuck with that exception as pointed out in code:

//this code is in some other class...     
et= (EditText) findViewById(R.id.searchcontact);
        et.addTextChangedListener(new TextWatcher() {

             public void onTextChanged(CharSequence s, int start, int before, int
             count) { // TODO Auto-generated method stub
                 lva.getFilter().filter(s);

RefreshListView rlv=new RefreshListView();<----class is getting called from here
                 lva.notifyDataSetInvalidated();
                 lva = new ListViewAdapterContacts(AllContactsActivity.this, rlv.names, rlv.types, rlv.phones, rlv.ids);

                 lv.setAdapter(lva);


             }

             public void beforeTextChanged(CharSequence s, int start, int count,
             int after) { // TODO Auto-generated method stub

             }

             public void afterTextChanged(Editable s) { 
                 // TODO Auto-generated  method stub


            // lva.notifyDataSetChanged();
             }
             });

...............********.........

public class RefreshListView extends ListActivity{

            String[] names,phones,types,ids;
            String name,id,phoneNumber;
            int type;
            ArrayList <String> newValues;

        public void onCreate(Bundle b){
        super.onCreate(b);

        newValues=SetNewList.getNames();
        getContacts();
    }



public void getContacts(){
                int foo = 0;


    ContentResolver cr = getContentResolver();<---//null pointer exception

    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                        null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
                        + ") ASC");
                if (cur.getCount() > 0) {

                    names = new String[cur.getCount()];
                    phones = new String[cur.getCount()];
                    types = new String[cur.getCount()];
                    ids = new String[cur.getCount()];

                    while (cur.moveToNext()) {

                        name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        for(int i=0;i<=newValues.size();i++){           
                            if(newValues.get(i).equalsIgnoreCase(name)) {
                                id = cur.getString(cur
                                        .getColumnIndex(ContactsContract.Contacts._ID));
                                String hasPhone = cur
                                .getString(cur
                                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                                if (Integer.parseInt(hasPhone) == 1) {

                                    Cursor phones = getContentResolver().query(
                                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = " + id, null, null);

                                    while (phones.moveToNext()) {
                                        phoneNumber = phones
                                        .getString(phones
                                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                                        type = phones.getInt(phones.getColumnIndex(Phone.TYPE));

                                    } // while

                                    phones.close();
                                }// if

                                else {
                                    phoneNumber = "unknown";
                                    type = 0;

                                }// else



                                phones[foo] = phoneNumber;
                                names[foo] = name;
                                ids[foo] = id;

                                if (type == 0) {
                                    String value = "unknown";
                                    types[foo] = value;
                                }

                                else if (type == 1) {
                                    String value = "home";
                                    types[foo] = value;
                                }// if

                                else if (type == 2) {
                                    String value = "mobile";
                                    types[foo] = value;
                                }// else if

                                else if (type == 3) {
                                    String value = "Work";
                                    types[foo] = value;
                                }

                                else if (type == 4) {
                                    String value = "Workfax";
                                    types[foo] = value;
                                }

                                else if (type == 5) {
                                    String value = "Home Fax";
                                    types[foo] = value;

                                } else if (type == 6) {
                                    String value = "Pager";
                                    types[foo] = value;
                                }

                                else {
                                    String value = "Other";

                                    types[foo] = value;
                                }

                                foo++;
                            }
                        }
                    }// while
                }// if


            }// get contacts

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:03:11+00:00Added an answer on May 27, 2026 at 7:03 am

    Don’t do this…

    public class RefreshListView extends ListActivity{
    
        public RefreshListView() {
    
            ...
    
        }
    }
    

    Do not use a constructor for an Android Activity!!!

    You should remove that constructor and put the code into the Activity onCreate(...) method.

    See the documentation for Activity and also read Activities.

    Try something like this…

    public class RefreshListView extends ListActivity{
    
        String[] names,phones,types,ids;
        String name,id,phoneNumber;
        int type;
        ArrayList <String> newValues;
        ContentResolver cr = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            cr = getContentResolver();
            newValues=SetNewList.getNames();//getnames is of static type
            getContacts();
        }
    
        public void getContacts() {
            int foo = 0;
    
            // REMOVE THE NEXT LINE !!!
            // ContentResolver cr = getContentResolver();
    
            ...
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been reading the android documentation on reflection, but i'm just not really grasping
I want to Reading Color from Android Application. Here I have developed one Application,
In my Android application for reading RSS links, I am getting this error: java.net.UnknownHostException:
I found a solution for reading epub books in android using epublib. I am
I was reading about Android, and it was an method ShowAlert, but a friend
After reading the android documentation about String, which includes this: This class is implemented
I am writing the program for reading contacts from android.when i am executing the
This is sort of out of curiosity question. Reading through Pro Android 3 book
I'm trying to build a conversation list for SMS messages. Therefore I'm reading content://sms/conversations
I've been reading the android api for capture video and reading some code and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.