java - Status variable returns nullPointerException (Android) -


i have been following tutorial (http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/) in order make listview display nearest restaurant location in radius of 5km.

however keep getting error, in runnable says status variable null. don't understand why error pops up. can guys give me short explanation , find solution??

placeslist.java

public class placeslist implements serializable {      public string status;     public list<place> results;  } 

displaylocations.java

public class displaylocations extends activity {      boolean isinternetpresent = false;      connectiondetector cd;     googleplaces googleplaces;     placeslist nearplaces;     gpstracker gps;      button shownonmap;     progressdialog pdialog;      listview lv;      arraylist<hashmap<string, string>> placeslistitems = new arraylist<hashmap<string, string>>();      public static string key_reference = "reference";     public static string key_name = "name";     public static string key_vicinity = "vicinity";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_display_locations);          cd = new connectiondetector(getapplicationcontext());          isinternetpresent = cd.isconnectingtointernet();         if(!isinternetpresent){             toast.maketext(getapplicationcontext(), "get working connection", toast.length_short).show();             return;         }          gps = new gpstracker(displaylocations.this);          if(gps.cangetlocation()){          }else{             gps.showsettingsalert();         }          lv = (listview)findviewbyid(r.id.list);         shownonmap = (button)findviewbyid(r.id.btn_show_map);          new loadplaces().execute();          shownonmap.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 /*                 intent = new intent(getapplicationcontext(), placesmapactivity.class);                 i.putextra("user_latitude", double.tostring(gps.getlatitude()));                 i.putextra("user_longitude", double.tostring(gps.getlongitude()));                 i.putextra("near_places", nearplaces);                 startactivity(i);                 */             }         });          lv.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                 string reference = ((textview) findviewbyid(r.id.reference)).gettext().tostring();                 /*                 intent in = new intent(getapplicationcontext(), singeplaceactivity.class);                 in.putextra(key_reference, reference);                 startactivity(in);                 */             }         });     }       class loadplaces extends asynctask<string, string, string>{          @override         protected void onpreexecute() {             super.onpreexecute();             pdialog = new progressdialog(displaylocations.this);             pdialog.setmessage(html.fromhtml("<b>search</b><br/>loading places..."));             pdialog.setindeterminate(false);             pdialog.setcancelable(false);             pdialog.show();         }          @override         protected string doinbackground(string... params) {             googleplaces = new googleplaces();              try{                 string types = "cafe|restaurant";                 double radius = 5000;                 nearplaces = googleplaces.search(gps.getlatitude(), gps.getlongitude(), radius, types);              }catch (exception e){                 e.printstacktrace();             }             return null;         }          @override         protected void onpostexecute(string file_url) {             pdialog.dismiss();              runonuithread(new runnable() {                 @override                 public void run() {                     string status = nearplaces.status;                      if (status.equals("ok")){                         if(nearplaces.results != null){                             (place p : nearplaces.results){                                 hashmap<string, string> map = new hashmap<string, string>();                                 map.put(key_reference, p.reference);                                 map.put(key_name, p.name);                                 placeslistitems.add(map);                             }                             listadapter adapter = new simpleadapter(displaylocations.this, placeslistitems, r.layout.location_listview_item,                                     new string[]{key_reference, key_name}, new int[]{r.id.reference, r.id.name});                             lv.setadapter(adapter);                         }                     }                     else if(status.equals("zero_results")){                         // 0 results found                         toast.maketext(getapplicationcontext(), "no results", toast.length_short);                     }                     else if(status.equals("unknown_error"))                     {                         toast.maketext(getapplicationcontext(), "unknown error", toast.length_short);                     }                     else if(status.equals("over_query_limit"))                     {                         toast.maketext(getapplicationcontext(), "query limit reached !", toast.length_short);                     }                     else if(status.equals("request_denied"))                     {                         toast.maketext(getapplicationcontext(), "request denied", toast.length_short);                     }                     else if(status.equals("invalid_request"))                     {                         toast.maketext(getapplicationcontext(), "invalid request", toast.length_short);                     }                     else                     {                         toast.maketext(getapplicationcontext(), "error", toast.length_short);                     }                 }             });         }     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_display_locations, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     } } 

googleplaces.java

public class googleplaces {      private static final httptransport http_transport = new nethttptransport();      private final string api_key = "aizasycxy1x7hc7soab7v3gbwd1u42fgthgyohg";      //googleplaces search url     private static final string places_search_url = "https://maps.googleapis.com/maps/api/place/search/json?";     private static final string places_text_search_url = "https://maps.googleapis.com/maps/api/place/search/json?";     private static final string places_details_url = "https://maps.googleapis.com/maps/api/place/details/json?";      private double mlatitiude;     private double mlongitude;     private double mradius;      public placeslist search(double latitude, double longitude, double radius, string types) throws exception{             this.mlatitiude = latitude;             this.mlongitude = longitude;             this.mradius = radius;              try {                 httprequestfactory httprequestfactory = createrequestfactory(http_transport);                 httprequest request = httprequestfactory.buildgetrequest(new genericurl(places_search_url));                 request.geturl().put("key", api_key);                 request.geturl().put("location", mlatitiude + "," + mlongitude);                 request.geturl().put("radius", mradius); // in meters                 request.geturl().put("sensor", "false");                 if (types != null)                     request.geturl().put("types", types);                  placeslist list = request.execute().parseas(placeslist.class);                 // check log cat places response status                 log.d("places status", "" + list.status);                 return list;              } catch (httpresponseexception e) {                 log.e("error:", e.getmessage());                 return null;             }      }      public placedetails getplacedetails(string reference) throws exception{         try{             httprequestfactory httprequestfactory = createrequestfactory(http_transport);             httprequest request = httprequestfactory.buildgetrequest(new genericurl(places_details_url));             request.geturl().put("reference", reference);             request.geturl().put("key", api_key);             request.geturl().put("sensor",false);              placedetails place = request.execute().parseas(placedetails.class);             return place;          }catch (httpresponseexception e){             throw e;         }     }      public static httprequestfactory createrequestfactory(final httptransport transport){         return transport.createrequestfactory(new httprequestinitializer() {             @override             public void initialize(httprequest request) throws ioexception {                 httpheaders httpheaders = new httpheaders();                 httpheaders.setuseragent("application test");                 request.setheaders(httpheaders);                     jsonobjectparser parser = new jsonobjectparser(new jsonfactory() {                         @override                         public jsonparser createjsonparser(inputstream in) throws ioexception {                             return null;                         }                          @override                         public jsonparser createjsonparser(inputstream in, charset charset) throws ioexception {                             return null;                         }                          @override                         public jsonparser createjsonparser(string value) throws ioexception {                             return null;                         }                          @override                         public jsonparser createjsonparser(reader reader) throws ioexception {                             return null;                         }                          @override                         public jsongenerator createjsongenerator(outputstream out, charset enc) throws ioexception {                             return null;                         }                          @override                         public jsongenerator createjsongenerator(writer writer) throws ioexception {                             return null;                         }                     });                     request.setparser(parser);             }          });     } } 

line 132 of displaylocations.java :

string status = nearplaces.status; 

this stacktrace :

 java.lang.nullpointerexception: attempt read field 'java.lang.string com.example.dell.exampleapplication.placeslist.status' on null object reference         @ com.example.dell.exampleapplication.displaylocations$loadplaces$1.run(displaylocations.java:132)         @ android.app.activity.runonuithread(activity.java:5517)         @ com.example.dell.exampleapplication.displaylocations$loadplaces.onpostexecute(displaylocations.java:129)         @ com.example.dell.exampleapplication.displaylocations$loadplaces.onpostexecute(displaylocations.java:98)         @ android.os.asynctask.finish(asynctask.java:632)         @ android.os.asynctask.access$600(asynctask.java:177)         @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645)         @ android.os.handler.dispatchmessage(handler.java:102)         @ android.os.looper.loop(looper.java:145)         @ android.app.activitythread.main(activitythread.java:5834)         @ java.lang.reflect.method.invoke(native method)         @ java.lang.reflect.method.invoke(method.java:372)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1388)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1183) 

any , explanation appreciated.

thanks, have nice day.

edit:

nearplaces.status = ... (the status: googleplaces) in doinbackground

then can do: string status = nearplaces.status; in onpostexecute


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -