android - java.lang.NullPointerException in AsyncTask -
i'm trying "optimize" app's code using asyctask, receive java.lang.nullpointerexception error.
can please me figure out i'm doing wrong?
package it.vscreazioni.farmachimica; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.list; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import org.w3c.dom.document; import org.w3c.dom.nodelist; import org.xml.sax.saxexception; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.widget.listview; import com.actionbarsherlock.app.sherlockactivity; import com.actionbarsherlock.view.menu; public class mostratutti extends sherlockactivity { final listview lv = (listview) findviewbyid(r.id.listview); final list<listviewitem> items = new arraylist<mostratutti.listviewitem>(); final arraylist<string> nome = new arraylist<string>(); final arraylist<string> immagine = new arraylist<string>(); ... ... final int array_image2[] ={r.drawable.iodocloroidrossichinolina,r.drawable.acidoacetilsalicilico, r.drawable.acidoascorbico,r.drawable.acidobarbiturico,r.drawable.acidobenzoico,...}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mostra_tutti); getsupportactionbar().setdisplayshowhomeenabled(false); rssfeedtask rsstask = new rssfeedtask(); rsstask.execute(); } private class rssfeedtask extends asynctask<string, void, string> { private progressdialog dialog; string response = ""; @override protected void onpreexecute() { dialog = new progressdialog(mostratutti.this); dialog.setmessage("leggo..."); dialog.show(); } @override protected string doinbackground(string... urls) { inputstream xmlfile = getresources().openrawresource(r.raw.sostanze); try { documentbuilderfactory documentbuilderfactory = documentbuilderfactory.newinstance(); documentbuilder documentbuilder = documentbuilderfactory.newdocumentbuilder(); final document document = documentbuilder.parse(xmlfile); document.getdocumentelement().normalize(); nodelist nodelist = document.getelementsbytagname("sostanza"); (int = 0; < nodelist.getlength(); i++) { final int indice = i; nome.add(document.getelementsbytagname("nome").item(indice).gettextcontent()); iupac.add(document.getelementsbytagname("iupac").item(indice).gettextcontent()); aspetto.add(document.getelementsbytagname("aspetto").item(indice).gettextcontent()); formula.add(document.getelementsbytagname("formula").item(indice).gettextcontent()); immagine.add(document.getelementsbytagname("immagine").item(indice).gettextcontent()); appartenenza.add(document.getelementsbytagname("appartenenza").item(indice).gettextcontent()); spiegazione.add(document.getelementsbytagname("spiegazione").item(indice).gettextcontent()); tempfus.add(document.getelementsbytagname("temperaturafusione").item(indice).gettextcontent()); tempeboll.add(document.getelementsbytagname("temperaturaebollizione").item(indice).gettextcontent()); solubilita.add(document.getelementsbytagname("solubilita").item(indice).gettextcontent()); note.add(document.getelementsbytagname("eccezioni").item(indice).gettextcontent()); string str = document.getelementsbytagname("formula").item(indice).gettextcontent(); str = str.replaceall("0", "\u2080"); str = str.replaceall("1", "\u2081"); str = str.replaceall("2", "\u2082"); str = str.replaceall("3", "\u2083"); str = str.replaceall("4", "\u2084"); str = str.replaceall("5", "\u2085"); str = str.replaceall("6", "\u2086"); str = str.replaceall("7", "\u2087"); str = str.replaceall("8", "\u2088"); str = str.replaceall("9", "\u2089"); final string stringa = str; formulaconvertita.add(stringa); items.add(new listviewitem() {{ thumbnailresource = array_image2[indice]; title = document.getelementsbytagname("nome").item(indice).gettextcontent(); subtitle = stringa; }}); }; } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (parserconfigurationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } return response; } @override protected void onpostexecute(string result) { customlistviewadapter adapter = new customlistviewadapter(mostratutti.this,items); lv.setadapter(adapter); /* _adapter = new customlistviewadapter(news.this,rssparser.items); lv=(listview)findviewbyid(r.id.list); lv.setadapter(_adapter); dialog.dismiss(); lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view v, int position, long id) { context context = getbasecontext(); intent myintent = new intent(context, leggiarticolo.class); myintent.putextra("testo",rssparser.testo.get(position)); myintent.putextra("titolo",rssparser.titolo.get(position)); myintent.putextra("immagine",rssparser.immagini.get(position)); myintent.putextra("link",rssparser.indirizzo.get(position)); startactivityforresult(myintent, 0); } } ); */ } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getsupportmenuinflater().inflate(r.menu.activity_main, menu); return true; } class listviewitem { public int thumbnailresource; public string title; public string subtitle; } }
and error:
04-25 11:43:57.872: w/dalvikvm(1663): threadid=1: thread exiting uncaught exception (group=0x40a13300) 04-25 11:43:57.902: e/androidruntime(1663): fatal exception: main 04-25 11:43:57.902: e/androidruntime(1663): java.lang.runtimeexception: unable instantiate activity componentinfo{it.vscreazioni.farmachimica/it.vscreazioni.farmachimica.mostratutti}: java.lang.nullpointerexception 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.activitythread.performlaunchactivity(activitythread.java:1983) 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2084) 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.activitythread.access$600(activitythread.java:130) 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.activitythread$h.handlemessage(activitythread.java:1195) 04-25 11:43:57.902: e/androidruntime(1663): @ android.os.handler.dispatchmessage(handler.java:99) 04-25 11:43:57.902: e/androidruntime(1663): @ android.os.looper.loop(looper.java:137) 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.activitythread.main(activitythread.java:4745) 04-25 11:43:57.902: e/androidruntime(1663): @ java.lang.reflect.method.invokenative(native method) 04-25 11:43:57.902: e/androidruntime(1663): @ java.lang.reflect.method.invoke(method.java:511) 04-25 11:43:57.902: e/androidruntime(1663): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 04-25 11:43:57.902: e/androidruntime(1663): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 04-25 11:43:57.902: e/androidruntime(1663): @ dalvik.system.nativestart.main(native method) 04-25 11:43:57.902: e/androidruntime(1663): caused by: java.lang.nullpointerexception 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.activity.findviewbyid(activity.java:1825) 04-25 11:43:57.902: e/androidruntime(1663): @ it.vscreazioni.farmachimica.mostratutti.<init>(mostratutti.java:25) 04-25 11:43:57.902: e/androidruntime(1663): @ java.lang.class.newinstanceimpl(native method) 04-25 11:43:57.902: e/androidruntime(1663): @ java.lang.class.newinstance(class.java:1319) 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.instrumentation.newactivity(instrumentation.java:1053) 04-25 11:43:57.902: e/androidruntime(1663): @ android.app.activitythread.performlaunchactivity(activitythread.java:1974) 04-25 11:43:57.902: e/androidruntime(1663): ... 11 more
can please me?
edit after answer:
public class mostratutti extends sherlockactivity { listview lv; final list<listviewitem> items = new arraylist<mostratutti.listviewitem>(); final arraylist<string> nome = new arraylist<string>(); ... final int array_image2[] ={r.drawable.iodocloroidrossichinolina,r.drawable.acidoacetilsalicilico, r.drawable.acidoascorbico,r.drawable.acidobarbiturico,r.drawable.acidobenzoico, ... }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mostra_tutti); lv = (listview) findviewbyid(r.id.listview); getsupportactionbar().setdisplayshowhomeenabled(false); rssfeedtask rsstask = new rssfeedtask(); rsstask.execute(); } private class rssfeedtask extends asynctask<string, void, string> { private progressdialog dialog; string response = ""; @override protected void onpreexecute() { dialog = new progressdialog(mostratutti.this); dialog.setmessage("leggo le sostanze..."); dialog.show(); } @override protected string doinbackground(string... urls) { inputstream xmlfile = getresources().openrawresource(r.raw.sostanze); try { documentbuilderfactory documentbuilderfactory = documentbuilderfactory.newinstance(); documentbuilder documentbuilder = documentbuilderfactory.newdocumentbuilder(); final document document = documentbuilder.parse(xmlfile); document.getdocumentelement().normalize(); //taghandler.handlechanneltag(document); nodelist nodelist = document.getelementsbytagname("sostanza"); (int = 0; < nodelist.getlength(); i++) { final int indice = i; nome.add(document.getelementsbytagname("nome").item(indice).gettextcontent()); iupac.add(document.getelementsbytagname("iupac").item(indice).gettextcontent()); aspetto.add(document.getelementsbytagname("aspetto").item(indice).gettextcontent()); formula.add(document.getelementsbytagname("formula").item(indice).gettextcontent()); immagine.add(document.getelementsbytagname("immagine").item(indice).gettextcontent()); appartenenza.add(document.getelementsbytagname("appartenenza").item(indice).gettextcontent()); spiegazione.add(document.getelementsbytagname("spiegazione").item(indice).gettextcontent()); tempfus.add(document.getelementsbytagname("temperaturafusione").item(indice).gettextcontent()); tempeboll.add(document.getelementsbytagname("temperaturaebollizione").item(indice).gettextcontent()); solubilita.add(document.getelementsbytagname("solubilita").item(indice).gettextcontent()); note.add(document.getelementsbytagname("eccezioni").item(indice).gettextcontent()); string str = document.getelementsbytagname("formula").item(indice).gettextcontent(); str = str.replaceall("0", "\u2080"); str = str.replaceall("1", "\u2081"); str = str.replaceall("2", "\u2082"); str = str.replaceall("3", "\u2083"); str = str.replaceall("4", "\u2084"); str = str.replaceall("5", "\u2085"); str = str.replaceall("6", "\u2086"); str = str.replaceall("7", "\u2087"); str = str.replaceall("8", "\u2088"); str = str.replaceall("9", "\u2089"); final string stringa = str; formulaconvertita.add(stringa); items.add(new listviewitem() {{ thumbnailresource = array_image2[indice]; title = document.getelementsbytagname("nome").item(indice).gettextcontent(); subtitle = stringa; }}); }; } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (parserconfigurationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } return response; } @override protected void onpostexecute(string result) { dialog.dismiss(); customlistviewadapter adapter = new customlistviewadapter(mostratutti.this,items); lv.setadapter(adapter); lv.setonitemclicklistener( new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view v, int position, long id) { context context = getbasecontext(); intent myintent = new intent(context, dettagli.class); myintent.putextra("nome_sostanza",nome.get(position)); // myintent.putextra("formula",formula.get(position)); myintent.putextra("iupac",iupac.get(position)); myintent.putextra("aspetto",aspetto.get(position)); myintent.putextra("appartenenza",appartenenza.get(position)); myintent.putextra("solubilita",solubilita.get(position)); myintent.putextra("tempfus",tempfus.get(position)); myintent.putextra("tempeboll",tempeboll.get(position)); myintent.putextra("spiegazione",spiegazione.get(position)); myintent.putextra("immagine", array_image2[position]); myintent.putextra("formulaconvertita", formulaconvertita.get(position)); myintent.putextra("note", note.get(position)); startactivityforresult(myintent, 0); } } ); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getsupportmenuinflater().inflate(r.menu.activity_main, menu); return true; } class listviewitem { public int thumbnailresource; public string title; public string subtitle; } }
and error's gone! there's strange behaviour: in simulator take's longer "normal" way:
16 seconds not async , 1 minute async...
you trying view before activity create:
final listview lv = (listview) findviewbyid(r.id.listview);
put line in oncreate function after setcontentview.