android - NullPointerException: Google Spreadsheet, doInBackground -


i nullpointerexception , can't figure out what's wrong. i've tried bring necessary code. have 3 classes: mainactivity, googlecommunicator , customadapter. error caused following in customadapter:

mactivity.updatebought(position, "1"); 

the errors line 283 , 277 are:

283: url listfeedurl = mworksheet.getlistfeedurl(); 277: private class updatebought extends asynctask<void, void, string> 

the logcat:

3011-3026/com.example.andb.apop_l6_google_communicator_app e/androidruntime﹕ fatal exception: asynctask #1 process: com.example.andb.apop_l6_google_communicator_app, pid: 3011 java.lang.runtimeexception: error occured while executing doinbackground()         @ android.os.asynctask$3.done(asynctask.java:300)         @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355)         @ java.util.concurrent.futuretask.setexception(futuretask.java:222)         @ java.util.concurrent.futuretask.run(futuretask.java:242)         @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231)         @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)         @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587)         @ java.lang.thread.run(thread.java:818)  caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.net.url com.google.gdata.data.spreadsheet.worksheetentry.getlistfeedurl()' on null object reference         @ com.example.andb.apop_l6_google_communicator_app.googlecommunicator$updatebought.doinbackground(googlecommunicator.java:283)         @ com.example.andb.apop_l6_google_communicator_app.googlecommunicator$updatebought.doinbackground(googlecommunicator.java:277)         @ android.os.asynctask$2.call(asynctask.java:288)         @ java.util.concurrent.futuretask.run(futuretask.java:237)         @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231)         @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)         @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 

          

mainactivity

public class mainactivity extends actionbaractivity implements adapterview.onitemclicklistener{      public googlecommunicator mgcom = new googlecommunicator(this,"torprode@gmail.com");     textview tvstatus;     edittext etadd;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          tvstatus = (textview) findviewbyid(r.id.tvstatus);          dosomegooglestuff();         buttonlistener();         update();      }      private void dosomegooglestuff(){         mgcom.setupfeed("mandatoryproject","buyme");     }       private void drawlistview() {         listadapter listadapter = new customadapter(this, mgcom.listitem, mgcom.listbought);         listview listview = (listview) findviewbyid(r.id.lv_items);         listview.setadapter(listadapter);         }       public void updatebought(int name, string bought) {         mgcom.updatebought(name, bought);     }       @override     public void onitemclick(adapterview<?> parent, view view, int position, long id) {      } } 

googlecommunicator

public class googlecommunicator {      //spreadsheet communication     private static final string mscope = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://spreadsheets.google.com/feeds https://docs.google.com/feeds";     private mainactivity mactivity;     private spreadsheetservice mspreadsheetservice;     private spreadsheetfeed mfeed;     private string mspreadsheetname;     private string mworksheetname;     private spreadsheetentry mspreadsheet;     private worksheetentry mworksheet;      private string itemname;     private int itemnameindex;     private string itembought;      //constructor     public googlecommunicator(mainactivity activity, string email) {         memail = email;         mactivity = activity; //possibility callback method in activity class      }       //method called application.     //creates instance of setupfeedtask (an asynctask) , executes     public void setupfeed(string spreadsheet_name, string worksheet_name){          mspreadsheetname = spreadsheet_name;         mworksheetname = worksheet_name;          new setupfeedtask().execute();     }      public void updatebought(int name, string bought) {         itemnameindex = name;         itembought = bought;         new updatebought().execute();     }      //asynctask handles network comminucation e.t.c.     private class setupfeedtask extends asynctask<void, void, string> {          //executes in own "worker thread" , doesnt block main ui thread         @override protected string doinbackground(void... params) {              // work             mtoken = fetchtoken();              mspreadsheetservice = new spreadsheetservice("myspreadsheetservice");             mspreadsheetservice.setauthsubtoken(mtoken);              url feed_url;             try {                 feed_url = new url("https://spreadsheets.google.com/feeds/spreadsheets/private/full");                 mfeed = mspreadsheetservice.getfeed(feed_url, spreadsheetfeed.class);             }catch(malformedurlexception e){                 //todo: handle exception                 log.v(tag, "malformedurlexception");                 return null;             }catch(serviceexception e){                 //todo: handle exception                 log.v(tag, "serviceexception");                 return null;             }catch(ioexception e){                 //todo: handle exception                 log.v(tag, "ioexception");                 return null;             }              try{                  list<spreadsheetentry> spreadsheets = mfeed.getentries();                  // iterate through of spreadsheets returned                 (spreadsheetentry spreadsheet : spreadsheets) {                     if (spreadsheet.gettitle().getplaintext().equals(mspreadsheetname)) {                         list<worksheetentry> worksheets = spreadsheet.getworksheets();                         //iterate through worksheets                         (worksheetentry worksheet : worksheets) {                             if (worksheet.gettitle().getplaintext().equals(mworksheetname)) {                                 mspreadsheet = spreadsheet;                                 mworksheet = worksheet;                                  log.v(tag,"spreadsheet , worksheet setup.");                              }                         }                     }                 }              }catch(serviceexception e){                 //todo: handle exception                 log.v(tag, "service exception");                 return null;             }catch(ioexception e){                 //todo: handle exception                 log.v(tag, "io exception");                 return null;             }              //just example.. mtoken not important return             return mtoken;          }          //call called when doinbackground has finished.         //executes in main ui thread         @override protected void onpostexecute(string result) {              //todo: notify rest of application, e.g.:             // * send broadcast             // * send message handler             // * call method on activity         }          //helper method         private string fetchtoken(){             try {                 return googleauthutil.gettoken(mactivity, memail, mscope);             } catch (userrecoverableauthexception userrecoverableexception) {                 // googleplayservices.apk either old, disabled, or not present,                 // recoverable, need show user ui through activity.                  //todo:                 if(mactivity instanceof mainactivity){                     ((mainactivity)mactivity).handleexception(userrecoverableexception);                     if(d) log.e(tag,"userrecoverableauthexception");                  }             } catch (googleauthexception fatalexception) {                  //todo:                 //onerror("unrecoverable error " + fatalexception.getmessage(), fatalexception);                 if(d) log.e(tag,"googleauthexception");             } catch (ioexception ioexception){                  if(d) log.e(tag,"ioexception");             }              return null;         }      }      private class updatebought extends asynctask<void, void, string> {          @override         protected string doinbackground(void... params) {             try {                  url listfeedurl = mworksheet.getlistfeedurl();                 listfeed listfeed = mspreadsheetservice.getfeed(listfeedurl, listfeed.class);                  listentry row = listfeed.getentries().get(itemnameindex);                  row.getcustomelements().setvaluelocal("bought", itembought);                  row.update();              } catch (ioexception e) {                 e.printstacktrace();             } catch (serviceexception e) {                 e.printstacktrace();             }              return null;         }      } } 

customadapter

class customadapter extends arrayadapter<string> {      arraylist boughtlist;     mainactivity mactivity = new mainactivity();      customadapter(context context, arraylist<string> item, arraylist<string> bought) {         super(context, r.layout.custom_listview, item);         boughtlist = bought;     }      @override     public view getview(final int position, view convertview, final viewgroup parent) {         layoutinflater listviewinflater = (layoutinflater.from(getcontext()));         final view customview = listviewinflater.inflate(r.layout.custom_listview, parent, false);          final string fooditem = getitem(position);         textview foodtext = (textview) customview.findviewbyid(r.id.tv_item);         final checkbox checkbox = (checkbox) customview.findviewbyid(r.id.cb_checked);          foodtext.settext(fooditem);          string foodbought = string.valueof(boughtlist.get(position));         int foodboughtint = integer.parseint(foodbought);         if (foodboughtint == 1) {             checkbox.setchecked(true);         } else {             checkbox.setchecked(false);         }          checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                 if (checkbox.ischecked()) {                     system.out.println("jep");                     mactivity.updatebought(position, "1");                 } else {                     system.out.println("nope");                     mactivity.updatebought(position, "0");                 }             }         });          return customview;     } } 

you encountering race condition. have asynchronous tasks execute, , second asynchronous task depends on first task done work correctly. because both tasks done asynchronously, done in background, on separate threads. setupfeed method not done working, , start updatebought method on new thread. happens updatebought begins while mworksheet still null. have reorganize code logic avoid race condition. have done in past when have 2 async tasks put second asynchronous task in onpostexecute() of first async task, because onpostexecute occurs once doinbackground finished.

here execellent article on asynctasks , threads developer guides.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -