Null Object Reference Error: Android Studio using SQLite -
so i'm trying adapt tutorial: http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
to own project. basic register & login in android/java using sqlite & volley sql server via php. can tutorial written working no problem. however, upon transferring project encounter crash;
05-12 01:18:05.763 1965-1965/com.disclosure_scots.disclosure_scots e/androidruntime? fatal exception: main process: com.disclosure_scots.disclosure_scots, pid: 1965 java.lang.nullpointerexception: attempt invoke virtual method 'void com.disclosure_scots.disclosure_scots.appcontroller.addtorequestqueue(com.android.volley.request, java.lang.string)' on null object reference @ com.disclosure_scots.disclosure_scots.registeractivity.registeruser(registeractivity.java:194) @ com.disclosure_scots.disclosure_scots.registeractivity.access$300(registeractivity.java:34) @ com.disclosure_scots.disclosure_scots.registeractivity$1.onclick(registeractivity.java:90) @ android.view.view.performclick(view.java:4780) @ android.view.view$performclick.run(view.java:19866) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5257) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
the line within registeractivity refers is;
appcontroller.getinstance().addtorequestqueue(strreq, tag_string_req); }
the activities involved are;
registeractivity
package com.disclosure_scots.disclosure_scots; import android.app.progressdialog; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import com.disclosure_scots.disclosure_scots.appconfig; import com.disclosure_scots.disclosure_scots.appcontroller; import com.disclosure_scots.disclosure_scots.sqlitehandler; import com.disclosure_scots.disclosure_scots.sessionmanager; import org.json.jsonexception; import org.json.jsonobject; import java.util.hashmap; import java.util.map; import android.view.view; import android.widget.button; import android.widget.checkbox; import android.widget.edittext; import android.widget.toast; import com.android.volley.request.method; import com.android.volley.response; import com.android.volley.volleyerror; import com.android.volley.toolbox.stringrequest; public class registeractivity extends actionbaractivity { private static final string tag = registeractivity.class.getsimplename(); private button btnregister; private edittext inputfullname; private edittext inputemail; private edittext inputpassword; private edittext inputtel_no; private edittext inputhome_add; private edittext inputpostcode; private checkbox inputpostal; private progressdialog pdialog; private sessionmanager session; private sqlitehandler db; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register); inputfullname = (edittext) findviewbyid(r.id.edittextname); inputemail = (edittext) findviewbyid(r.id.edittextemail); inputpassword = (edittext) findviewbyid(r.id.edittextpass); inputtel_no = (edittext) findviewbyid(r.id.edittextphone); inputhome_add = (edittext) findviewbyid(r.id.edittextpost); inputpostcode = (edittext) findviewbyid(r.id.edittextpostcode); inputpostal = (checkbox) findviewbyid(r.id.postalcheckbox); btnregister = (button) findviewbyid(r.id.btnregister); // progress dialog pdialog = new progressdialog(this); pdialog.setcancelable(false); // session manager session = new sessionmanager(getapplicationcontext()); // sqlite database handler db = new sqlitehandler(getapplicationcontext()); // check if user logged in or not if (session.isloggedin()) { // user logged in. take him legal disclaimer activity intent intent = new intent(registeractivity.this, legaldiscactivity.class); startactivity(intent); finish(); } // register button click event btnregister.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { string name = inputfullname.gettext().tostring(); string email = inputemail.gettext().tostring(); string password = inputpassword.gettext().tostring(); if (!name.isempty() && !email.isempty() && !password.isempty()) { registeruser(name, email, password); } else { toast.maketext(getapplicationcontext(), "please enter details!", toast.length_long) .show(); } } }); // link login screen /*btnlinktologin.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent = new intent(getapplicationcontext(), loginactivity.class); startactivity(i); finish(); } });*/ } /** * function store user in mysql database post params(tag, name, * email, password) register url * */ private void registeruser(final string name, final string email, final string password) { // tag used cancel request string tag_string_req = "req_register"; pdialog.setmessage("registering ..."); showdialog(); stringrequest strreq = new stringrequest(method.post, appconfig.url_register, new response.listener<string>() { @override public void onresponse(string response) { log.d(tag, "register response: " + response.tostring()); hidedialog(); try { jsonobject jobj = new jsonobject(response); boolean error = jobj.getboolean("error"); if (!error) { // user stored in mysql // store user in sqlite string uid = jobj.getstring("uid"); jsonobject user = jobj.getjsonobject("user"); string name = user.getstring("name"); string email = user.getstring("email"); string created_at = user .getstring("created_at"); // inserting row in users table db.adduser(name, email, uid, created_at); // launch login activity intent intent = new intent( registeractivity.this, loginactivity.class); startactivity(intent); finish(); } else { // error occurred in registration. error // message string errormsg = jobj.getstring("error_msg"); toast.maketext(getapplicationcontext(), errormsg, toast.length_long).show(); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.e(tag, "registration error: " + error.getmessage()); toast.maketext(getapplicationcontext(), error.getmessage(), toast.length_long).show(); hidedialog(); } }) { @override protected map<string, string> getparams() { // posting params register url map<string, string> params = new hashmap<string, string>(); params.put("tag", "register"); params.put("name", name); params.put("email", email); params.put("password", password); return params; } }; // adding request request queue appcontroller.getinstance().addtorequestqueue(strreq, tag_string_req); } private void showdialog() { if (!pdialog.isshowing()) pdialog.show(); } private void hidedialog() { if (pdialog.isshowing()) pdialog.dismiss(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_register, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
appcontroller class
package com.disclosure_scots.disclosure_scots; /** * created administrator on 09/05/2015. */ import android.app.application; import android.text.textutils; import com.android.volley.request; import com.android.volley.requestqueue; import com.android.volley.toolbox.volley; public class appcontroller extends application { public static final string tag = appcontroller.class.getsimplename(); private requestqueue mrequestqueue; private static appcontroller minstance; @override public void oncreate() { super.oncreate(); minstance = this; } public static synchronized appcontroller getinstance() { return minstance; } public requestqueue getrequestqueue() { if (mrequestqueue == null) { mrequestqueue = volley.newrequestqueue(getapplicationcontext()); } return mrequestqueue; } public <t> void addtorequestqueue(request<t> req, string tag) { req.settag(textutils.isempty(tag) ? tag : tag); getrequestqueue().add(req); } public <t> void addtorequestqueue(request<t> req) { req.settag(tag); getrequestqueue().add(req); } public void cancelpendingrequests(object tag) { if (mrequestqueue != null) { mrequestqueue.cancelall(tag); } } }
i've been on number of times cannot tell why getting null object in copied version while original works fine. appreciated.
this because didn't register appcontroller in manifest getinstance
returns null.
Comments
Post a Comment