java - arrayList String search: NullPointerException -
i seek explain best can. (i'm still new both java , android)
issue:
i trying compare incoming number string contact object's number string searching arraylist.
background:
i able load contacts arraylist different views (listview, textview, etc) know methods , objects working. it's new class (ringerservice) i'm having issue with.
design
i have arraylist of contacts in class named contactstorage. works intended displaying different views:
//constructor context access project resources , instantiate jsonfile arraylist private contactstorage(context appcontext){ mappcontext = appcontext; mserializer = new contactjsoner(mappcontext, filename); try{ mcontacts = mserializer.loadcontacts(); }catch (exception e){ mcontacts = new arraylist<contact>(); log.e(tag, "no contacts available, creating new list: ", e); } } //get method homecoming 1 instance constructor public static contactstorage get(context c){ if (scontactstorage == null){ scontactstorage = new contactstorage(c.getapplicationcontext()); } homecoming scontactstorage; } //for ringer service find matching number public contact getcontactnumber(string number){ (contact c: mcontacts){ if(c.getnumber().replaceall("[^0-9]", "").equals(number)) homecoming c; } homecoming null; }
when phone call get method above in ringerservice class below, that's when things break. specific, getting nullpointerexception on oncallstatechanged:
private contact mcontact; private string number; private context mcontext; @override public void oncreate(){ mtelephonymanager = (telephonymanager)getsystemservice(context.telephony_service); mphonestatelistener = new phonestatelistener(){ // state alter @override public void oncallstatechanged(int state, string incomingnumber){ if (state == 1 ){ try{ mcontact = contactstorage.get(mcontext).getcontactnumber(incomingnumber); number = mcontact.getnumber(); log.d(tag, state+" received incoming number: " + number); }catch(exception e){ log.d(tag, " exception: " + e); } } else { log.d(tag, state+" number not found" + incomingnumber); } } }; super.oncreate(); }
troubeshooting:
1. i've removed reference number (number = mcontact.getnumber();) - programme runs fine in case. can send test phone call emulator , log message displays correctly test number arg. considered way in array searching works in getcontactnumber class. never finding matching value, resulting in null?
2. thought since service, i'm somehow not getting right context when calling contactstorage.get(context c) method.
3. if set mcontact reference , no number match found, mcontact = null; still allow programme run?
you trying match strings ==
in c.getnumber() == number
check if 2 objects reference equals
use c.getnumber().equals(number)
java android arraylist nullpointerexception singleton
No comments:
Post a Comment