how to remove null pointerException in android? -
@override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub if(!constantvariable.isfilteroccured){ view v = convertview; if (v == null) { layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); v = minflater.inflate(r.layout.row_listitem, null); } final textview platformname = (textview) v .findviewbyid(r.id.item_platform); final textview schdepart = (textview) v .findviewbyid(r.id.item_schdepart); final textview expdepart = (textview) v .findviewbyid(r.id.item_expectdepart); final textview arrival = (textview) v .findviewbyid(r.id.item_arrival); final textview exparrival = (textview) v .findviewbyid(r.id.item_expertarrival); final textview stationname = (textview) v .findviewbyid(r.id.item_stationname); final imageview alertstatusimage = (imageview) v .findviewbyid(r.id.alertstatus); final string platformvalue = deparaturedaseboarddto.get(position).getplatformno(); final string schdepartvalue= deparaturedaseboarddto.get(position).getschdepart(); final string schexpectvalue= deparaturedaseboarddto.get(position).getexpdepart(); final string arrivalvalue= deparaturedaseboarddto.get(position).getdestscharrival(); final string exparrivalvalue= deparaturedaseboarddto.get(position).getdestscharrival(); string stationnamevalue= deparaturedaseboarddto.get(position).getdestinationstation().getstationname(); if(deparaturedaseboarddto.get(position).getalertsid()!=0){ alertstatusimage.setvisibility(view.visible); }else { alertstatusimage.setvisibility(view.invisible); } platformname.settext(platformvalue); schdepart.settext(schdepartvalue); expdepart.settext(schexpectvalue); arrival.settext(arrivalvalue); exparrival.settext(exparrivalvalue); stationname.settext(stationnamevalue); if (position%2==0) { log.d("--------", "old"); v.setbackgroundcolor(color.parsecolor("#ffffff")); } else { log.d("--------", "even"); v.setbackgroundcolor(color.parsecolor("#f3f3f3")); } homecoming v; }else { view vv = convertview; if (vv == null) { layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); vv = minflater.inflate(r.layout.filterdashboard, null); } final textview platformname = (textview) vv .findviewbyid(r.id.platformid); final textview destinationname = (textview) vv .findviewbyid(r.id.destinationstationname); final textview filterstationname = (textview) vv .findviewbyid(r.id.filterstationnameid); final textview toscharrival = (textview) vv .findviewbyid(r.id.toscharrivalid); final textview toexparrival = (textview) vv .findviewbyid(r.id.toexparrivalid); final textview destinationscharrival = (textview) vv .findviewbyid(r.id.destscharrivalid); final textview destinationexparrival = (textview) vv .findviewbyid(r.id.destexparrivalid); final textview schdepart = (textview) vv .findviewbyid(r.id.schdepartid); final textview expdepart = (textview) vv .findviewbyid(r.id.expdepartid); final imageview alertstatusimage = (imageview) vv .findviewbyid(r.id.alertstatus); final string platformvalue = deparaturedaseboarddto.get(position).getplatformno(); final string toscharrivalvalue= deparaturedaseboarddto.get(position).gettoscharrival(); final string toexparrivalvalue= deparaturedaseboarddto.get(position).gettoexparrival(); final string destscharrivalvalue= deparaturedaseboarddto.get(position).getdestscharrival(); final string destexparrivalvalue= deparaturedaseboarddto.get(position).getdestexparrival(); final string schdepartvalue= deparaturedaseboarddto.get(position).getschdepart(); final string expdepartvalue= deparaturedaseboarddto.get(position).getexpdepart(); string destinationstationnamvalue= deparaturedaseboarddto.get(position).getdestinationstation().getstationname(); string filterstationnamevalue= deparaturedaseboarddto.get(position).getfilteredstation().getstationname(); if(deparaturedaseboarddto.get(position).getalertsid()!=0){ alertstatusimage.setvisibility(view.visible); }else { alertstatusimage.setvisibility(view.invisible); } platformname.settext(platformvalue); destinationname.settext(destinationstationnamvalue); filterstationname.settext(filterstationnamevalue); toscharrival.settext(toscharrivalvalue); toexparrival.settext(toexparrivalvalue); destinationscharrival.settext(destscharrivalvalue); destinationexparrival.settext(destexparrivalvalue); schdepart.settext(schdepartvalue); expdepart.settext(expdepartvalue); if (position%2==0) { log.d("--------", "old"); vv.setbackgroundcolor(color.parsecolor("#ffffff")); } else { log.d("--------", "even"); vv.setbackgroundcolor(color.parsecolor("#f3f3f3")); } homecoming vv; } }
hi getting null pointer exception.i know come when trying access null value .but don't know how remove .i explain problem .i have list view need show info on listview coming service.actully take 1 status if(!constantvariable.isfilteroccured){ intially isfilteroccured false .it goes within condition
if (v == null) { layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); v = minflater.inflate(r.layout.row_listitem, null); }
but when phone call webservice if(!constantvariable.isfilteroccured){ intially isfilteroccured true why not going within status reason getting null.
view vv = convertview; if (vv == null) { layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); vv = minflater.inflate(r.layout.filterdashboard, null); }
then not find item
final textview platformname = (textview) vv .findviewbyid(r.id.platformid);
please see update:
11-02 06:42:30.634: e/androidruntime(743): fatal exception: main 11-02 06:42:30.634: e/androidruntime(743): java.lang.nullpointerexception 11-02 06:42:30.634: e/androidruntime(743): @ com.firstgroup.components.departurelistadapter.getview(departurelistadapter.java:156) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.abslistview.obtainview(abslistview.java:2012) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.listview.measureheightofchildren(listview.java:1244) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.listview.onmeasure(listview.java:1155) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.view.measure(view.java:12603) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.relativelayout.measurechild(relativelayout.java:579) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.relativelayout.onmeasure(relativelayout.java:392) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.view.measure(view.java:12603) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4677) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.framelayout.onmeasure(framelayout.java:293) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.view.measure(view.java:12603) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4677) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1369) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.linearlayout.measurevertical(linearlayout.java:660) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.linearlayout.onmeasure(linearlayout.java:553) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.view.measure(view.java:12603) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4677) 11-02 06:42:30.634: e/androidruntime(743): @ android.widget.framelayout.onmeasure(framelayout.java:293) 11-02 06:42:30.634: e/androidruntime(743): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2072) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.view.measure(view.java:12603) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1044) 11-02 06:42:30.634: e/androidruntime(743): @ android.view.viewrootimpl.handlemessage(viewrootimpl.java:2418) 11-02 06:42:30.634: e/androidruntime(743): @ android.os.handler.dispatchmessage(handler.java:99) 11-02 06:42:30.634: e/androidruntime(743): @ android.os.looper.loop(looper.java:137) 11-02 06:42:30.634: e/androidruntime(743): @ android.app.activitythread.main(activitythread.java:4340) 11-02 06:42:30.634: e/androidruntime(743): @ java.lang.reflect.method.invokenative(native method) 11-02 06:42:30.634: e/androidruntime(743): @ java.lang.reflect.method.invoke(method.java:511) 11-02 06:42:30.634: e/androidruntime(743): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 11-02 06:42:30.634: e/androidruntime(743): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 11-02 06:42:30.634: e/androidruntime(743): @ dalvik.system.nativestart.main(native method)
i getting exception on line
platformname.settext(platformvalue);
because platformname null .i think null because id not utilize xml
layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); vv = minflater.inflate(r.layout.filterdashboard, null);
platformid defined in filterdashboard.xml
working that
public class departurelistadapter extends baseadapter{ arraylist<deparaturedaseboarddto> deparaturedaseboarddto; private context context; public departurelistadapter( context context, arraylist<deparaturedaseboarddto> deparaturedaseboarddto){ this.deparaturedaseboarddto=deparaturedaseboarddto; this.context = context; } @override public int getcount() { // todo auto-generated method stub homecoming deparaturedaseboarddto.size(); } @override public object getitem(int position) { // todo auto-generated method stub homecoming deparaturedaseboarddto.get(position); } @override public long getitemid(int position) { // todo auto-generated method stub homecoming 0; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub if(!constantvariable.isfilteroccured){ view v = convertview; if (v == null) { layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); v = minflater.inflate(r.layout.row_listitem, null); } final textview platformname = (textview) v .findviewbyid(r.id.item_platform); final textview schdepart = (textview) v .findviewbyid(r.id.item_schdepart); final textview expdepart = (textview) v .findviewbyid(r.id.item_expectdepart); final textview arrival = (textview) v .findviewbyid(r.id.item_arrival); final textview exparrival = (textview) v .findviewbyid(r.id.item_expertarrival); final textview stationname = (textview) v .findviewbyid(r.id.item_stationname); final imageview alertstatusimage = (imageview) v .findviewbyid(r.id.alertstatus); final string platformvalue = deparaturedaseboarddto.get(position).getplatformno(); final string schdepartvalue= deparaturedaseboarddto.get(position).getschdepart(); final string schexpectvalue= deparaturedaseboarddto.get(position).getexpdepart(); final string arrivalvalue= deparaturedaseboarddto.get(position).getdestscharrival(); final string exparrivalvalue= deparaturedaseboarddto.get(position).getdestscharrival(); string stationnamevalue= deparaturedaseboarddto.get(position).getdestinationstation().getstationname(); if(deparaturedaseboarddto.get(position).getalertsid()!=0){ alertstatusimage.setvisibility(view.visible); }else { alertstatusimage.setvisibility(view.invisible); } platformname.settext(platformvalue); schdepart.settext(schdepartvalue); expdepart.settext(schexpectvalue); arrival.settext(arrivalvalue); exparrival.settext(exparrivalvalue); stationname.settext(stationnamevalue); if (position%2==0) { log.d("--------", "old"); v.setbackgroundcolor(color.parsecolor("#ffffff")); } else { log.d("--------", "even"); v.setbackgroundcolor(color.parsecolor("#f3f3f3")); } homecoming v; }else { view vv = convertview; // if (vv == null) { // layoutinflater minflater = (layoutinflater) context // .getsystemservice(context.layout_inflater_service); // vv = minflater.inflate(r.layout.filterdashboard, null); // } layoutinflater minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); vv = minflater.inflate(r.layout.filterdashboard, null); final textview platformname = (textview) vv .findviewbyid(r.id.platformid); final textview destinationname = (textview) vv .findviewbyid(r.id.destinationstationname); final textview filterstationname = (textview) vv .findviewbyid(r.id.filterstationnameid); final textview toscharrival = (textview) vv .findviewbyid(r.id.toscharrivalid); final textview toexparrival = (textview) vv .findviewbyid(r.id.toexparrivalid); final textview destinationscharrival = (textview) vv .findviewbyid(r.id.destscharrivalid); final textview destinationexparrival = (textview) vv .findviewbyid(r.id.destexparrivalid); final textview schdepart = (textview) vv .findviewbyid(r.id.schdepartid); final textview expdepart = (textview) vv .findviewbyid(r.id.expdepartid); final imageview alertstatusimage = (imageview) vv .findviewbyid(r.id.alertstatusimage); final string platformvalue = deparaturedaseboarddto.get(position).getplatformno(); final string toscharrivalvalue= deparaturedaseboarddto.get(position).gettoscharrival(); final string toexparrivalvalue= deparaturedaseboarddto.get(position).gettoexparrival(); final string destscharrivalvalue= deparaturedaseboarddto.get(position).getdestscharrival(); final string destexparrivalvalue= deparaturedaseboarddto.get(position).getdestexparrival(); final string schdepartvalue= deparaturedaseboarddto.get(position).getschdepart(); final string expdepartvalue= deparaturedaseboarddto.get(position).getexpdepart(); string destinationstationnamvalue= deparaturedaseboarddto.get(position).getdestinationstation().getstationname(); string filterstationnamevalue= deparaturedaseboarddto.get(position).getfilteredstation().getstationname(); if(deparaturedaseboarddto.get(position).getalertsid()!=0){ alertstatusimage.setvisibility(view.visible); }else { alertstatusimage.setvisibility(view.invisible); } platformname.settext(platformvalue); destinationname.settext(destinationstationnamvalue); filterstationname.settext(filterstationnamevalue); toscharrival.settext(toscharrivalvalue); toexparrival.settext(toexparrivalvalue); destinationscharrival.settext(destscharrivalvalue); destinationexparrival.settext(destexparrivalvalue); schdepart.settext(schdepartvalue); expdepart.settext(expdepartvalue); if (position%2==0) { log.d("--------", "old"); vv.setbackgroundcolor(color.parsecolor("#ffffff")); } else { log.d("--------", "even"); vv.setbackgroundcolor(color.parsecolor("#f3f3f3")); } homecoming vv; } } public void referhlist( arraylist<deparaturedaseboarddto> deparaturedaseboarddto){ this.deparaturedaseboarddto=deparaturedaseboarddto; notifydatasetchanged(); } } private void showfilterwebdashboard(string response) { log.d("--",response); constantvariable.isfilteroccured=true; departureholder localdata = new gson().fromjson(response, departureholder.class); data=localdata; adapter.referhlist(data.getdata()); }
i recommend create layout list items contains both of current layouts r.layout.row_listitem , r.layout.filterdashboard (so you'll inflate r.layout.merged_layouts), , based on
constantvariable.isfilteroccured
you can alter visibility of corresponding container layout, , maintain view reusability (you add together viewholder pattern boosting performance).
hope helps.
android
No comments:
Post a Comment