android - ArrayAdapter is null even after getting a string array as a parameter -


i'm working on sunshine app in developing android app course udacity. stuck in lesson 2. i've listed down mainactivity.java contains listview populated network call in asynctask inner class in mainactivity.java. application crashes, due null pointer exception, array adapter null. i've tried debugging, , weekforecast (i.e., arraylist stores parsed data, , parameter creation of arrayadapter) have valid parsed data. in advance.

public class mainactivity extends appcompatactivity {  listview listview; arrayadapter<string> arrayadapter;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     listview=(listview)findviewbyid(r.id.listview_forecast);     gettingweatherfromnetwork gettingweatherfromnetwork = new gettingweatherfromnetwork();     gettingweatherfromnetwork.execute("94043");     listview.setadapter(arrayadapter); }  @override public boolean oncreateoptionsmenu(menu menu) {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.main,menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     int id = item.getitemid();     switch(id) {         case r.id.action_settings : return true;         case r.id.action_refresh : gettingweatherfromnetwork gettingweatherfromnetwork = new gettingweatherfromnetwork();             gettingweatherfromnetwork.execute("94043");             return true;     }     return super.onoptionsitemselected(item); }  public class gettingweatherfromnetwork extends asynctask<string, void, string[]> {     private final string log_tag = gettingweatherfromnetwork.class.getsimplename();     //removed api key. part of main code i'm running.     private final string api_key = "  ";      @override     protected string[] doinbackground(string... params) {         httpurlconnection urlconnection = null;         bufferedreader reader = null;          string forecastjsonstr = null;         string format = "json";         string units = "metric";         int noofdays = 7;          try {              final string base_url = "http://api.openweathermap.org/data/2.5/forecast/daily?";             final string query_param = "q";             final string mode_param = "mode";             final string units_param = "units";             final string count_param = "cnt";             final string key_param = "appid";              uri builturi = uri.parse(base_url).buildupon()                     .appendqueryparameter(query_param,params[0])                     .appendqueryparameter(mode_param,format)                     .appendqueryparameter(units_param,units)                     .appendqueryparameter(count_param, string.valueof(noofdays))                     .appendqueryparameter(key_param,api_key)                     .build();              string url = builturi.tostring();             url url = new url(url);              urlconnection = (httpurlconnection) url.openconnection();             urlconnection.setrequestmethod("get");             urlconnection.connect();              inputstream inputstream = urlconnection.getinputstream();             stringbuffer buffer = new stringbuffer();             if (inputstream == null) {                 return null;             }             reader = new bufferedreader(new inputstreamreader(inputstream));              string line;             while ((line = reader.readline()) != null) {                 buffer.append(line + "\n");             }              if (buffer.length() == 0) {                 return null;             }             forecastjsonstr = buffer.tostring();         } catch (ioexception e) {             log.e(log_tag, string.valueof(e));             return null;         } finally{             if (urlconnection != null) {                 urlconnection.disconnect();             }             if (reader != null) {                 try {                     reader.close();                 } catch (final ioexception e) {                     log.e(log_tag, string.valueof(e));                 }             }         }          string [] weatherforecast = new string[0];         weatherparser weatherparser = new weatherparser();         try {             weatherforecast = weatherparser.getweatherdatafromjson(forecastjsonstr,7);         } catch (jsonexception e) {             e.printstacktrace();         }         return weatherforecast;     }      @override     protected void onpostexecute(string[] s) {         list<string> weekforecast = new arraylist<string>(arrays.aslist(s));         arrayadapter = new arrayadapter<string>(getapplicationcontext(),r.layout.list_item_forecast,r.id.list_item_forecast_textview,weekforecast);     } } } 

try removing line

listview.setadapter(arrayadapter); 

from oncreate method , add in onpostexecute() method of asynctask, after initialising arrayadapter.

@override protected void onpostexecute(string[] s) {     list<string> weekforecast = new arraylist<string>(arrays.aslist(s));     arrayadapter = new arrayadapter<string>(getapplicationcontext(),r.layout.list_item_forecast,r.id.list_item_forecast_textview,weekforecast);      //set adapter set listview after initialzing it.      listview.setadapter(arrayadapter); } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -