android fetch contact information returns null pointer exception -


in android code

i want fetch contact's name,email , phone number as json , want display.

here code:

public class mainactivity extends activity {      public textview outputtext;     string[] phonenumber;     string[] email;     string name;     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         outputtext = (textview) findviewbyid(r.id.textview1);         try {             //fetchcontacts();             outputtext.settext(fetchcontacts());         } catch (jsonexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     }      public string fetchcontacts() throws jsonexception {          uri content_uri = contactscontract.contacts.content_uri;         string _id = contactscontract.contacts._id;         string display_name = contactscontract.contacts.display_name;         string has_phone_number = contactscontract.contacts.has_phone_number;          uri phonecontent_uri = contactscontract.commondatakinds.phone.content_uri;         string phone_contact_id = contactscontract.commondatakinds.phone.contact_id;         string number = contactscontract.commondatakinds.phone.number;          uri emailcontent_uri = contactscontract.commondatakinds.email.content_uri;         string emailcontact_id = contactscontract.commondatakinds.email.contact_id;         string data = contactscontract.commondatakinds.email.data;          contentresolver contentresolver = getcontentresolver();          cursor cursor = contentresolver.query(content_uri, null, null, null,                 null);          list<contact> contacts = new arraylist<contact>();         gson gson = new gson();          // loop every contact in phone         if (cursor.getcount() > 0) {             while (cursor.movetonext()) {                  string contact_id = cursor                         .getstring(cursor.getcolumnindex(_id));                 name = cursor.getstring(cursor.getcolumnindex(display_name));                  int hasphonenumber = integer.parseint(cursor.getstring(cursor                         .getcolumnindex(has_phone_number)));                  if (hasphonenumber > 0) {                      int p = 0;                     // query , loop every phone number of contact                     cursor phonecursor = contentresolver.query(                             phonecontent_uri, null, phone_contact_id + " = ?",                             new string[] { contact_id }, null);                      while (phonecursor.movetonext()) {                         phonenumber[p] = phonecursor.getstring(phonecursor                                 .getcolumnindex(number));                         p++;                     }                     phonecursor.close();                      int q = 0;                     // query , loop every email of contact                     cursor emailcursor = contentresolver.query(                             emailcontent_uri, null, emailcontact_id + " = ?",                             new string[] { contact_id }, null);                      while (emailcursor.movetonext()) {                          email[q] = emailcursor.getstring(emailcursor                                 .getcolumnindex(data));                         q++;                     }                     emailcursor.close();                      contacts.add(new contact(name, phonenumber, email));                 }             }         }         return gson.tojson(contacts);     } } 

but getting nullpointer exception error :

java.lang.runtimeexception: unable start activity  componentinfo{com.example.contactdemo/com.example.contactdemo.mainactivity}: java.lang.nullpointerexception 

error found in below line of code:

phonenumber[p] = phonecursor.getstring(phonecursor.getcolumnindex(number)); 

here storing phonenumber , email array.is there array initialization problem ?? idea guys?

you can use these approach finds contact contact list

 class fetchdevicecontact extends asynctask<void, integer, string>     {         protected void onpreexecute (){             constant.showprogressdialog(adddevicecontactscreeen.this);         }          protected string doinbackground(void...arg0) {               arraylist.clear();               contentresolver cr = adddevicecontactscreeen.this.getcontentresolver();             cursor cur = adddevicecontactscreeen.this.getcontentresolver().query(contactscontract.contacts.content_uri, null, null, null, null);              while (cur.movetonext()) {                   string id = cur.getstring(cur.getcolumnindex(contactscontract.data._id));                  string name = cur.getstring(cur.getcolumnindex(contactscontract.data.display_name));                  string photouri = cur.getstring(cur.getcolumnindex(contactscontract.data.photo_thumbnail_uri));                  bitmap my_btmp = bitmapfactory.decoderesource(getresources(), r.drawable.ic_arrow_up_blue);                   string email = null;                 string phoneno = null;                  cursor phonecur = adddevicecontactscreeen.this.getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null,                         contactscontract.commondatakinds.phone.contact_id + " = ?",                         new string[]{id}, null);                   if (photouri != null) {                     uri my_contact_uri = uri.parse(photouri);                     try {                          my_btmp = mediastore.images.media.getbitmap(adddevicecontactscreeen.this.getcontentresolver(), my_contact_uri);                       } catch (filenotfoundexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     } catch (ioexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                  }                   if (phonecur.getcount() > 0) {                       while (phonecur.movetonext()) {                           phoneno = phonecur.getstring(phonecur.getcolumnindex(contactscontract.commondatakinds.phone.number));                       }                   }                  cursor emailcur = cr.query(                         contactscontract.commondatakinds.email.content_uri, null,                         contactscontract.commondatakinds.email.contact_id + " = ?",                         new string[]{id}, null);                  while (emailcur.movetonext()) {                     // contact names //                = cur1.getstring(cur1.getcolumnindex(contactscontract.commondatakinds.email.data)                     email = emailcur.getstring(emailcur.getcolumnindex(contactscontract.commondatakinds.email.data));                       if (email != null) {                           system.out.println("email============== :" + email);                      }                  }                 emailcur.close();                  contactbean bean = new contactbean();                  bean.setname(name);                 bean.setemail(email);                 bean.setimage(my_btmp);                 bean.setphone_number(phoneno);                  if (phoneno == null || email == null) {                  } else {                      arraylist.add(bean);                 }              }              return "";         }          protected void onprogressupdate(integer...a){          }          protected void onpostexecute(string result) {               constant.canceldialog();            }     }    new fetchdevicecontact().execute(); 

create getter , setter class it:-

public class contactbean {      string name;     string email;     bitmap image;     string phone_number;      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public string getemail() {         return email;     }      public void setemail(string email) {         this.email = email;     }      public bitmap getimage() {         return image;     }      public void setimage(bitmap image) {         this.image = image;     }      public string getphone_number() {         return phone_number;     }      public void setphone_number(string phone_number) {         this.phone_number = phone_number;     }  } 

Comments