android - Passing Aysnc Task Results to parent Calling Class -
i need pass async task result calling class. have created separate async class called other classes. passing response async task in "post execute" method calling class method getting null point exception. below calling method in
public boolean getcategories() { seruri = "categories.json"; webserviceasynctask webservicetask = new webserviceasynctask(); webservicetask.execute(seruri,this); return true; }
the method executed result below aysnc task
public void writejsonarray(final jsonarray result) { try { (int i=0; i<result.length();i++){ jsonobject c = result.getjsonobject(i); string name = c.getstring("catname"); } } catch (jsonexception e) { e.printstacktrace(); } }
webserviceasynctask class:
public class webserviceasynctask extends asynctask<object,void,jsonarray> { romsjson roms; private static jsonarray json = null; private context context = null; protected jsonarray doinbackground(object... params) { // todo auto-generated method stub string serviceurl = (string) params[0]; final httphelper httph = new httphelper(serviceurl,context); if(serviceurl.equalsignorecase("categories.json")) { json = httph.fetch(); }else if(serviceurl.equalsignorecase("categories/create")) { } return json; } @override protected void onpostexecute(jsonarray result) { // invoked on ui thread roms.writejsonarray(result); super.onpostexecute(result); }
i getting null point exception when roms.writejsonarray(result)
called. result correctly received before command. checked log statement. if write writejsonarray method in async class instead of calling class, works fine.
i not sure if missing in passing result or while calling methods. please advise. thanks.
null pointer exception
because roms null
you declaring romsjson roms;
inside webserviceasynctask
not initializing !
and using inside `onpostexecute(jsonarray result)
roms.writejsonarray(result);` // here roms in null
so initialize roms
before using !
Comments
Post a Comment