java - Getting NullPointerException in code which was working earlier -
i have code check pnr number. because of internet issues, thought give users check pnr sms. added 2 radio buttons, 1 internet , 1 sms. problem when click pnr button, gives nullpointer exception. here main activity.java
public class mainactivity extends activity implements onclicklistener { /** called when activity first created. */ private edittext pnrnumber; private textview errmsg; private button getpnr; private button pnrclear; button yes, no; radiobutton checkbyinternet, checkbysms; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); errmsg = (textview) findviewbyid(r.id.errmsg); pnrnumber = (edittext) findviewbyid(r.id.pnrnumber_p01); getpnr = (button) findviewbyid(r.id.checkpnrbutton); pnrclear = (button) findviewbyid(r.id.pnrclear); getpnr.setonclicklistener(this); pnrclear.setonclicklistener(this); } public void onclick(view src) { // perform action on click if (src.getid() == r.id.checkpnrbutton) { if (checkbyinternet.ischecked()) { int pnr2 = pnrnumber.geteditabletext().length(); if (pnr2 != 10) { errmsg.settext("length of pnr invalid."); } else { string pnr = pnrnumber.geteditabletext().tostring(); bundle b = new bundle(); b.putstring("pnr", pnr); system.out.println("connectivity : " + this.isnetworkavailable()); pnrstatus pnrstatus = null; // connect server , pnr status try { string captcha = "37819"; string pnr1 = pnrnumber.gettext().tostring(); string reqstr = "lccp_pnrno1=" + pnr1 + "&lccp_cap_val=" + captcha + "&lccp_capinp_val=" + captcha + "&submitpnr=get+status"; pnrstatuscheck check = new pnrstatuscheck(); stringbuffer data = check .getpnrresponse(reqstr, "http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_28688.cgi"); // string pnr1 = pnr; //"1154177041"; // string reqstr = "lccp_pnrno1=" + pnr1 + // "&submitpnr=get+status"; // pnrstatuscheck check = new pnrstatuscheck(); // stringbuffer data = check.getpnrresponse(reqstr, // "http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi"); if (data != null) { pnrstatus = check.parsehtml(data); b.putserializable("pnrstatus", pnrstatus); } else { // error } } catch (exception e) { e.printstacktrace(); } intent = null; if (pnrstatus != null) { = new intent(this, pnrstatusactivity.class); to.putextras(b); startactivity(to); } else { errmsg.settext("error prcessing pnr. please try again."); } } } else if (checkbysms.ischecked()) { // toast.maketext(this, "sms", toast.length_short).show(); int pnr2 = pnrnumber.geteditabletext().length(); if (pnr2 != 10) { errmsg.settext("length of pnr invalid."); } else { opensmswarningdialog(src); } } } else if (src.getid() == r.id.pnrclear) { errmsg.settext(""); pnrnumber.settext(""); } } public boolean isnetworkavailable() { connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo networkinfo = cm.getactivenetworkinfo(); // if no network available networkinfo null, otherwise check // if connected if (networkinfo != null && networkinfo.isconnected()) { return true; } return false; } public void opensmswarningdialog(view view) { final dialog dialog = new dialog(mainactivity.this); dialog.setcontentview(r.layout.smsdialog); dialog.settitle("are sure use sms.?"); yes = (button) dialog.findviewbyid(r.id.yes); no = (button) dialog.findviewbyid(r.id.no); yes.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub string pnrnum = pnrnumber.gettext().tostring(); string messagetosend = ("pnr " + pnrnum); string number = "139"; smsmanager.getdefault().sendtextmessage(number, null, messagetosend, null, null); dialog.dismiss(); toast.maketext( getbasecontext(), "please check inbox in sometime pnr status", toast.length_long).show(); } }); no.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub dialog.dismiss(); } }); dialog.show(); } }
and here layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:background="@drawable/background" android:gravity="center_horizontal" android:orientation="vertical" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margintop="17dp" android:layout_marginleft="7dp" android:layout_marginright="7dp" android:text="@string/title" android:gravity="center_horizontal" android:textcolor="@android:color/black" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/errmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="17dp" android:layout_marginleft="7dp" android:layout_marginright="7dp" android:textcolor="@android:color/black" android:text="10 digits mandatory" /> <edittext android:id="@+id/pnrnumber_p01" android:layout_width="match_parent" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:paddingleft="10dp" android:background="@drawable/edittextellipsedbackground" android:layout_margintop="17dp" android:layout_marginbottom="7dp" android:layout_marginleft="7dp" android:layout_marginright="7dp" android:hint="@string/pnrtextview" > <requestfocus /> </edittext> <button android:id="@+id/checkpnrbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="@drawable/bluebutton" android:layout_margintop="17dp" android:layout_marginleft="7dp" android:layout_marginright="7dp" android:padding="10dp" android:shadowcolor="#000000" android:shadowradius="5.9" android:text="@string/checkpnrbutton" android:textcolor="#ffffff" android:textsize="20sp" /> <button android:id="@+id/pnrclear" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bluebutton" android:layout_margintop="7dp" android:layout_marginleft="7dp" android:layout_marginright="7dp" android:padding="10dp" android:shadowcolor="#000000" android:shadowradius="5.9" android:text="clear" android:textcolor="#ffffff" android:textsize="20sp" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="7dp" android:layout_marginright="7dp" android:layout_margintop="20dp" android:background="@drawable/roundlayoutborder" android:gravity="center" android:paddingbottom="5dp" > <radiogroup android:id="@+id/checkvia" android:layout_width="wrap_content" android:layout_height="100dp" android:orientation="vertical" > <radiobutton android:id="@+id/internet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textcolor="@android:color/black" android:text="@string/checkbyinternet" android:textappearance="?android:attr/textappearancesmall" /> <radiobutton android:id="@+id/sms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="@android:color/black" android:text="@string/checkbysms" android:textappearance="?android:attr/textappearancesmall" /> </radiogroup> </linearlayout> </linearlayout>
sms layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#9bafb0" android:orientation="vertical" > <textview android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:layout_margintop="10dp" android:textcolor="#ff0000" android:ems="27" android:text="@string/checkthroughsmswarning" > </textview> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_margintop="15dp" android:layout_marginbottom="15dp" > <button android:id="@+id/yes" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginleft="5dp" android:layout_marginright="2dp" android:textcolor="@android:color/white" android:layout_weight="1" android:background="@drawable/bluebutton" android:text="yes" /> <button android:id="@+id/no" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginleft="2dp" android:layout_marginright="5dp" android:textcolor="@android:color/white" android:layout_weight="1" android:background="@drawable/bluebutton" android:text="no" /> </linearlayout> </linearlayout>
here log:
02-06 13:54:22.810: e/androidruntime(858): fatal exception: main 02-06 13:54:22.810: e/androidruntime(858): java.lang.nullpointerexception 02-06 13:54:22.810: e/androidruntime(858): @ akshat.jaiswal.indianrailways.mainactivity.onclick(mainactivity.java:50) 02-06 13:54:22.810: e/androidruntime(858): @ android.view.view.performclick(view.java:4084) 02-06 13:54:22.810: e/androidruntime(858): @ android.view.view$performclick.run(view.java:16966) 02-06 13:54:22.810: e/androidruntime(858): @ android.os.handler.handlecallback(handler.java:615) 02-06 13:54:22.810: e/androidruntime(858): @ android.os.handler.dispatchmessage(handler.java:92) 02-06 13:54:22.810: e/androidruntime(858): @ android.os.looper.loop(looper.java:137) 02-06 13:54:22.810: e/androidruntime(858): @ android.app.activitythread.main(activitythread.java:4745) 02-06 13:54:22.810: e/androidruntime(858): @ java.lang.reflect.method.invokenative(native method) 02-06 13:54:22.810: e/androidruntime(858): @ java.lang.reflect.method.invoke(method.java:511) 02-06 13:54:22.810: e/androidruntime(858): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 02-06 13:54:22.810: e/androidruntime(858): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 02-06 13:54:22.810: e/androidruntime(858): @ dalvik.system.nativestart.main(native method)
please help, not able solution this.
your checkbyinternet
, checkbysms
buttons uninitialized , why you're getting nullpointerexception
when if (checkbyinternet.ischecked())
executed in onclick()
method.
if (src.getid() == r.id.checkpnrbutton) // true if pressed getpnr button { if (checkbyinternet.ischecked()) // checkbyinternet uninitialized yet, it'll throw npe
you need initialize them in oncreate()
method.
checkbyinternet = (radiobutton) findviewbyid(r.id.internet); checkbysms = (radiobutton) findviewbyid(r.id.sms);
Comments
Post a Comment