java - nullPointerException in Android can't handle it -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
this working code somehow started make nullpointerexception.
i errors
java.lang.nullpointerexception @ com.ahmetyuva.deniyorum.ui.mainactivity.startfilmactivity(mainactivity.java:228) @ com.ahmetyuva.deniyorum.ui.mainactivity$$viewinjector$1.doclick(mainactivity$$viewinjector.java:16) @ butterknife.internal.debouncingonclicklistener.onclick(debouncingonclicklistener.java:22) @ android.view.view.performclick(view.java:4204) @ android.view.view$performclick.run(view.java:17355) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5041) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) @ dalvik.system.nativestart.main(native method)
this mainactivity.class
package com.ahmetyuva.deniyorum.ui; import android.content.context; import android.content.intent; import android.graphics.drawable.drawable; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.util.log; import android.view.view; import android.widget.imageview; import android.widget.progressbar; import android.widget.textview; import android.widget.toast; import com.ahmetyuva.deniyorum.r; import com.parse.parse; import com.parse.parseexception; import com.parse.parseobject; import com.parse.parsepush; import com.parse.parsepushbroadcastreceiver; import com.parse.savecallback; import com.squareup.okhttp.call; import com.squareup.okhttp.callback; import com.squareup.okhttp.okhttpclient; import com.squareup.okhttp.request; import com.squareup.okhttp.response; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.ioexception; import butterknife.butterknife; import butterknife.injectview; import butterknife.onclick; import com.ahmetyuva.deniyorum.r; import com.ahmetyuva.deniyorum.weather.film; import com.ahmetyuva.deniyorum.weather.forecast; public class mainactivity extends actionbaractivity { public static final string tag = mainactivity.class.getsimplename(); public static final string film_forecast = "film_forecast"; static string image = "image"; private forecast mforecast; /* @injectview(r.id.timelabel) textview mtimelabel; @injectview(r.id.temperaturelabel) textview mtemperaturelabel; @injectview(r.id.humidityvalue) textview mhumidityvalue; @injectview(r.id.precipvalue) textview mprecipvalue; @injectview(r.id.summarylabel) textview msummarylabel; */ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.inject(this); /* parse.initialize(this, "9zrtd9hdt9eu80r1glcwrgqeloeagyz7b6oud32t", "4nkjg1invdsotdnm1mts8ogntznmta9lxihunw0k"); parseobject testobject = new parseobject("testobject"); testobject.put("foo", "bar"); testobject.saveinbackground(); parsepush.subscribeinbackground("", new savecallback() { @override public void done(parseexception e) { if (e == null) { log.d("com.parse.push", "successfully subscribed broadcast channel."); } else { log.e("com.parse.push", "failed subscribe push", e); } } }); */ getforecast(); log.d(tag, "main ui code running!"); } /* public class receiver extends parsepushbroadcastreceiver { @override public void onpushopen(context context, intent intent) { intent = new intent(context, mainactivity.class); i.putextras(intent.getextras()); i.setflags(intent.flag_activity_new_task); context.startactivity(i); } } */ private void getforecast() { string forecasturl = "http://ahmetyuva.com/elada.json"; if (isnetworkavailable()) { okhttpclient client = new okhttpclient(); request request = new request.builder() .url(forecasturl) .build(); call call = client.newcall(request); call.enqueue(new callback() { @override public void onfailure(request request, ioexception e) { runonuithread(new runnable() { @override public void run() { } }); alertuserabouterror(); } @override public void onresponse(response response) throws ioexception { runonuithread(new runnable() { @override public void run() { } }); try { string jsondata = response.body().string(); log.v(tag, jsondata); if (response.issuccessful()) { mforecast = parseforecastdetails(jsondata); runonuithread(new runnable() { @override public void run() { } }); } else { alertuserabouterror(); } } catch (ioexception e) { log.e(tag, "exception caught: ", e); } catch (jsonexception e) { log.e(tag, "exception caught: ", e); } } }); } else { toast.maketext(this, getstring(r.string.network_unavailable_message), toast.length_long).show(); } } private forecast parseforecastdetails(string jsondata) throws jsonexception { forecast forecast = new forecast(); forecast.setfilmforecast(getfilmforecast(jsondata)); return forecast; } private film[] getfilmforecast(string jsondata) throws jsonexception { jsonobject forecast = new jsonobject(jsondata); jsonarray contacts = forecast.getjsonarray("contacts"); film[] filmler = new film[contacts.length()]; (int = 0; < contacts.length(); i++) { jsonobject jsonfilm = contacts.getjsonobject(i); film film = new film(); film.setname(jsonfilm.getstring("name")); film.setemail(jsonfilm.getstring("email")); film.setaddress(jsonfilm.getstring("address")); film.setgender(jsonfilm.getstring("gender")); film.setimage(jsonfilm.getstring("image")); filmler[i] = film; } return filmler; } private boolean isnetworkavailable() { connectivitymanager manager = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo networkinfo = manager.getactivenetworkinfo(); boolean isavailable = false; if (networkinfo != null && networkinfo.isconnected()) { isavailable = true; } return isavailable; } private void alertuserabouterror() { alertdialogfragment dialog = new alertdialogfragment(); dialog.show(getfragmentmanager(), "error_dialog"); } @onclick (r.id.filmbutton) public void startfilmactivity(view view) { intent intent = new intent(this, filmforecastactivity.class); intent.putextra(film_forecast, mforecast.getfilmforecast()); startactivity(intent); } }
this filmadapter
package com.ahmetyuva.deniyorum.adapters; /** * created ahmetyuva on 24/03/15. */ import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import com.ahmetyuva.deniyorum.r; import com.ahmetyuva.deniyorum.weather.film; import com.squareup.picasso.callback; import com.squareup.picasso.picasso; import android.widget.baseadapter; import com.android.volley.toolbox.imageloader; /** * created benjakuben on 2/6/15. */ public class filmadapter extends baseadapter { private context mcontext; private film[] mfilmler; private imageloader imageloader; public filmadapter(context context, film[] days) { mcontext = context; mfilmler = days; } @override public int getcount() { return mfilmler.length; } @override public object getitem(int position) { return mfilmler[position]; } @override public long getitemid(int position) { return 0; // aren't going use this. tag items easy reference } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { // brand new layoutinflater layoutinflater = layoutinflater.from(getcontext()); convertview = layoutinflater.from(mcontext).inflate(r.layout.film_list_item, null); holder = new viewholder(); holder.namelabel = (textview) convertview.findviewbyid(r.id.namelabel); holder.emaillabel = (textview) convertview.findviewbyid(r.id.emaillabel); holder.genderlabel = (textview) convertview.findviewbyid(r.id.genderlabel); holder.addresslabel = (textview) convertview.findviewbyid(r.id.addresslabel); holder.imageid = (imageview) convertview.findviewbyid(r.id.imageid); convertview.settag(holder); holder.imageid.setimagedrawable(getcontext().getresources().getdrawable(r.drawable.bg_gradient)); } else { holder = (viewholder) convertview.gettag(); } film day = mfilmler[position]; /* byte[] decodedstring = base64.decode(string.valueof(holder.imageid), base64.url_safe); bitmap decodedbyte = bitmapfactory.decodebytearray(decodedstring, 0, decodedstring.length); */ holder.namelabel.settext(day.getname()); holder.emaillabel.settext(day.getemail()); holder.genderlabel.settext(day.getgender()); holder.addresslabel.settext(day.getaddress()); picasso.with(getcontext()) .load(day.getimage()) //.placeholder(r.drawable.admit_one) //.error(r.drawable.emoticon_sad) .into(holder.imageid, new callback() { @override public void onsuccess() { } @override public void onerror() { } } ); /* convertview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent =new intent(filmadapter.this, singlefilm.class); } }); */ /* convertview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { // selected item string product = ((textview) view).gettext().tostring(); // launching new activity on selecting single list item intent = new intent(getapplicationcontext(), singlelistitem.class); // sending data new activity i.putextra("product", product); startactivity(i); } }); */ return convertview; } public context getcontext() { return mcontext; } private static class viewholder { textview namelabel; // public default textview emaillabel; textview genderlabel; textview addresslabel; imageview imageid; } }
and filmforecastactivity.java
package com.ahmetyuva.deniyorum.ui; /** * created ahmetyuva on 24/03/15. */ import android.app.activity; import android.content.intent; import android.os.bundle; import android.os.parcelable; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.widget.textview; import android.widget.toast; import java.util.arrays; import butterknife.butterknife; import butterknife.injectview; import com.ahmetyuva.deniyorum.r; import com.ahmetyuva.deniyorum.adapters.filmadapter; import com.ahmetyuva.deniyorum.weather.film; public class filmforecastactivity extends activity { private film[] mfilmler; @injectview(android.r.id.list) listview mlistview; @injectview(android.r.id.empty) textview memptytextview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_film_forecast); butterknife.inject(this); intent intent = getintent(); parcelable[] parcelables = intent.getparcelablearrayextra(mainactivity.film_forecast); mfilmler = arrays.copyof(parcelables, parcelables.length, film[].class); filmadapter adapter = new filmadapter(this, mfilmler); mlistview.setadapter(adapter); mlistview.setemptyview(memptytextview); mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string name = mfilmler[position].getname(); string email = mfilmler[position].getemail(); string gender = mfilmler[position].getgender(); string address = mfilmler[position].getaddress(); string image = mfilmler[position].getimage(); intent myintent = new intent(filmforecastactivity.this,singlefilm.class); myintent.putextra("name", name); myintent.putextra("email", email); startactivity(myintent); } }); /* mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string name = mfilmler[position].getname(); string email = mfilmler[position].getemail(); string gender = mfilmler[position].getgender() + ""; string message = string.format("on %s high %s , %s", name, gender, email); toast.maketext(filmforecastactivity.this, message, toast.length_long).show(); } }); */ } }
your variable mforecast
null
@ point method startfilmactivity()
being called.
either make sure set before, example in method oncreate()
, or check null
before use it:
if(mforecast == null) { // handle issue } else { // right }
Comments
Post a Comment