Getting error while reading contacts in android development -
i'm new android development. i'm developing in eclipse , java. i'm trying read contacts , string manupulation substring() or contains (). did following steps,
- permission added ( )
code read contacts :
cursor people=getcontentresolver().query(contactscontract.contacts.content_uri, null, null, null, null); while(people.movetonext()) { int id=people.getcolumnindex(phonelookup.display_name); string name= people.getstring(id); if(name!="") { edittext1.settext(name.substring(2,3)); } }
here i'm getting error because of substring. i'm getting error if use string class methods. if assign hardcoded value in name,then i'm not getting error. ie.
string name= "john"; // people.getstring(id);
error messages :
08-19 22:13:03.467: e/androidruntime(4265): fatal exception: main 08-19 22:13:03.467: e/androidruntime(4265): java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1111, result=-1, data=intent { (has extras) }} activity {com.ravi.vrs/com.ravi.vrs.voice}: java.lang.nullpointerexception 08-19 22:13:03.467: e/androidruntime(4265): @ android.app.activitythread.deliverresults(activitythread.java:2553) 08-19 22:13:03.467: e/androidruntime(4265): @ android.app.activitythread.handlesendresult(activitythread.java:2595) 08-19 22:13:03.467: e/androidruntime(4265): @ android.app.activitythread.access$2000(activitythread.java:121) 08-19 22:13:03.467: e/androidruntime(4265): @ android.app.activitythread$h.handlemessage(activitythread.java:973) 08-19 22:13:03.467: e/androidruntime(4265): @ android.os.handler.dispatchmessage(handler.java:99) 08-19 22:13:03.467: e/androidruntime(4265): @ android.os.looper.loop(looper.java:130) 08-19 22:13:03.467: e/androidruntime(4265): @ android.app.activitythread.main(activitythread.java:3701) 08-19 22:13:03.467: e/androidruntime(4265): @ java.lang.reflect.method.invokenative(native method) 08-19 22:13:03.467: e/androidruntime(4265): @ java.lang.reflect.method.invoke(method.java:507) 08-19 22:13:03.467: e/androidruntime(4265): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:866) 08-19 22:13:03.467: e/androidruntime(4265): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:624) 08-19 22:13:03.467: e/androidruntime(4265): @ dalvik.system.nativestart.main(native method) 08-19 22:13:03.467: e/androidruntime(4265): caused by: java.lang.nullpointerexception 08-19 22:13:03.467: e/androidruntime(4265): @ com.ravi.vrs.voice.onactivityresult(voice.java:71) 08-19 22:13:03.467: e/androidruntime(4265): @ android.app.activity.dispatchactivityresult(activity.java:3908) 08-19 22:13:03.467: e/androidruntime(4265): @ android.app.activitythread.deliverresults(activitythread.java:2549) 08-19 22:13:03.467: e/androidruntime(4265): ... 11 more
could please guide me how fix issue ?
i'm assuming code fragment posted onactivityresult
method of class voice
. problem due contact not having defined display_name
, in case name
set null
. not checking null value name
. change code this:
if (name!="")
to:
if (name != null && name.length() > 0)
you should protecting against exceptions when call getstring
. the docs cursor.getstring
:
the result , whether method throws exception when column value null or column type not string type implementation-defined.
Comments
Post a Comment