-2

I am trying to pick the display name, mobile number from the contacts in phone and showing these in a list by using a list view. But I'm getting null pointer exception while running the following code:

public class SeeActivity extends ListActivity {
    public String [] allname;
       public String [] allnumber;
       public int listcount=0;
       public String [] details;
       String[] PROJECTION = new String[] {
               ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
               ContactsContract.CommonDataKinds.Phone.NUMBER




       };
      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


;

        Cursor phone= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,PROJECTION ,null ,null ,null);

       try{
        while(phone.moveToNext())
        {  

           String name= phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
           String number=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           allname[listcount]=name;
           allnumber[listcount]=number;
           details [listcount]=name+"    "+number;
           listcount=listcount+1;

        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,details);
        setListAdapter(adapter);
        ListView l=getListView();
           l.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getApplicationContext(),((TextView)arg1).getText() , Toast.LENGTH_SHORT).show();



           }
        });}
       catch( Exception e)
       {
           Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();   
       }



    }


}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Rabindra Nath Nandi
  • 1,433
  • 1
  • 15
  • 28
  • 1
    Where is the Stacktrace? (in which line is the NullPointerException thrown?) – MrSmith42 Dec 28 '12 at 19:37
  • you may want to read following first http://developer.android.com/guide/topics/providers/contacts-provider.html#RawContactBasics – David Dec 28 '12 at 19:41

2 Answers2

1

The problem is that you have declared arrays of String but you have not created/allocated memory for them:

public String [] allname;
public String [] allnumber;
public int listcount=0;
public String [] details;

So, when you are trying to access them like:

allname[listcount]=name;
allnumber[listcount]=number;
details [listcount]=name+" "+number;

it gives null pointer exception. So, create your String arrays while declaring them:

public String[] allname = new String[100];
public String[] allnumber = new String[100];
public int listcount = 0;
public String[] details = new String[100];

Have a look at arrays


And don't forget to close your cursor.

Imran Rana
  • 11,899
  • 7
  • 45
  • 51
0
    public String [] allname;
    public String [] allnumber;

are pointing to null; and following assignment results NullPointerException.

       allname[listcount]=name;
       allnumber[listcount]=number;

Change array creation statement something like:

public String[] allname = new String[10];
public String[] allnumber = new String[10];

If you don't know size ahead of time, I would suggest using something like ArrayList instead of array.

kosa
  • 65,990
  • 13
  • 130
  • 167