java - Getting IndexOutOfBound Android -
i have made registration demo login functionality. while login credentials, give me indexoutofboundexception. below required code & error log you.
login.java
package com.example.registrationexample; import java.util.arraylist; import android.r.string; import android.support.v7.app.actionbaractivity; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class login extends actionbaractivity { edittext edt_txt_username, edt_txt_pwdactual; button btn_loginactual; arraylist<regusergetset> singleuserdata; sharedpreferences sp; public static string sharedpref = "sharedprefuserdata"; public static string userfullname = "fullnamekey"; public static string useremail = "emailkey"; public static string userpwd = "pwdkey"; public static string userdob = "dobkey"; public static string usergender = "genderkey"; databasehandler dbhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); edt_txt_username = (edittext) findviewbyid(r.id.edt_txt_loginemail); edt_txt_pwdactual = (edittext) findviewbyid(r.id.edt_txt_loginpwd); btn_loginactual = (button) findviewbyid(r.id.btn_loginactual); btn_loginactual.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub string username = edt_txt_username.gettext().tostring(); string pwd = edt_txt_pwdactual.gettext().tostring(); dbhandler = new databasehandler(getbasecontext()); singleuserdata = dbhandler.getuser(username, pwd); if (singleuserdata.size() == 1) { // sharedpref(arraylist < login > singleuserdata); sp = getsharedpreferences(sharedpref, context.mode_private); sharedpreferences.editor editor = sp.edit(); editor.putstring(userfullname, singleuserdata.get(0) .tostring()); editor.putstring(useremail, singleuserdata.get(1) .tostring()); editor.putstring(useremail, singleuserdata.get(2) .tostring()); editor.putstring(useremail, singleuserdata.get(3) .tostring()); editor.putstring(useremail, singleuserdata.get(4) .tostring()); editor.commit(); intent i_welcome = new intent(getapplicationcontext(), welcome.class); startactivity(i_welcome); toast.maketext(getapplicationcontext(), "you login...!!", toast.length_long).show(); } else { toast.maketext(getapplicationcontext(), "credentials not matched...!!", toast.length_long) .show(); } } private void sharedpref(arraylist<login> singleusersharedprefdata) { // todo auto-generated method stub } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.login, 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(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
databasehandler.java
package com.example.registrationexample; import java.util.arraylist; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class databasehandler extends sqliteopenhelper { static int db_version = 1; static string db_name = "users_info"; string table_name = "users"; string key_id = "user_id"; string key_full_name = "user_full_name"; string key_email = "user_email"; string key_pwd = "user_pwd"; string key_dob = "user_dob"; string key_gender = "user_gender"; public databasehandler(context context) { super(context, db_name, null, db_version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub string create_table = "create table " + table_name + "(" + key_id + " integer primary key, " + key_full_name + " text, " + key_email + " text, " + key_pwd + " text, " + key_dob + " text, " + key_gender + " text)"; db.execsql(create_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists " + table_name); oncreate(db); } public long registeruser(regusergetset regusergetset) { // todo auto-generated method stub sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_full_name, regusergetset.getfname() + " " + regusergetset.getlname()); values.put(key_email, regusergetset.getemail()); values.put(key_pwd, regusergetset.getpwd()); values.put(key_dob, regusergetset.getdob()); values.put(key_gender, regusergetset.getgender()); long result = db.insert(table_name, null, values); log.e("inserted data", "" + result); log.e("fname & lname", "" + regusergetset.getfname() + " " + regusergetset.getlname()); db.close(); return result; } public arraylist<regusergetset> getalluser() { // todo auto-generated method stub arraylist<regusergetset> allusers = new arraylist<regusergetset>(); string select_query = "select * " + table_name; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(select_query, null); if (cursor.movetofirst()) { { regusergetset singleuserobj = new regusergetset(); singleuserobj.setfname(cursor.getstring(cursor .getcolumnindex(key_full_name))); singleuserobj.setlname(cursor.getstring(cursor .getcolumnindex(key_full_name))); singleuserobj.setemail(cursor.getstring(cursor .getcolumnindex(key_email))); singleuserobj.setpwd(cursor.getstring(cursor .getcolumnindex(key_pwd))); singleuserobj.setdob(cursor.getstring(cursor .getcolumnindex(key_dob))); singleuserobj.setgender(cursor.getstring(cursor .getcolumnindex(key_gender))); allusers.add(singleuserobj); } while (cursor.movetonext()); } db.close(); cursor.close(); return allusers; } public arraylist<regusergetset> getuser(string username, string pwd) { // todo auto-generated method stub arraylist<regusergetset> singleuser = new arraylist<regusergetset>(); string select_user = "select * " + table_name + " " + key_email + "='" + username + "' , " + key_pwd + "='" + pwd + "'"; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(select_user, null); if (cursor.movetofirst()) { { regusergetset obj = new regusergetset(); // obj.setuser_id(cursor.getstring(cursor.getcolumnindex(key_id))); obj.setfname(cursor.getstring(cursor .getcolumnindex(key_full_name))); obj.setlname(cursor.getstring(cursor .getcolumnindex(key_full_name))); obj.setemail(cursor.getstring(cursor.getcolumnindex(key_email))); obj.setpwd(cursor.getstring(cursor.getcolumnindex(key_pwd))); obj.setdob(cursor.getstring(cursor.getcolumnindex(key_dob))); obj.setgender(cursor.getstring(cursor .getcolumnindex(key_gender))); singleuser.add(obj); } while (cursor.movetonext()); } db.close(); cursor.close(); return singleuser; } }
error log
07-02 13:34:22.924: e/androidruntime(631): fatal exception: main 07-02 13:34:22.924: e/androidruntime(631): java.lang.indexoutofboundsexception: invalid index 1, size 1 07-02 13:34:22.924: e/androidruntime(631): @ java.util.arraylist.throwindexoutofboundsexception(arraylist.java:257) 07-02 13:34:22.924: e/androidruntime(631): @ java.util.arraylist.get(arraylist.java:311) 07-02 13:34:22.924: e/androidruntime(631): @ com.example.registrationexample.login$1.onclick(login.java:63) 07-02 13:34:22.924: e/androidruntime(631): @ android.view.view.performclick(view.java:2408) 07-02 13:34:22.924: e/androidruntime(631): @ android.view.view$performclick.run(view.java:8816) 07-02 13:34:22.924: e/androidruntime(631): @ android.os.handler.handlecallback(handler.java:587) 07-02 13:34:22.924: e/androidruntime(631): @ android.os.handler.dispatchmessage(handler.java:92) 07-02 13:34:22.924: e/androidruntime(631): @ android.os.looper.loop(looper.java:123) 07-02 13:34:22.924: e/androidruntime(631): @ android.app.activitythread.main(activitythread.java:4627) 07-02 13:34:22.924: e/androidruntime(631): @ java.lang.reflect.method.invokenative(native method) 07-02 13:34:22.924: e/androidruntime(631): @ java.lang.reflect.method.invoke(method.java:521) 07-02 13:34:22.924: e/androidruntime(631): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 07-02 13:34:22.924: e/androidruntime(631): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 07-02 13:34:22.924: e/androidruntime(631): @ dalvik.system.nativestart.main(native method)
in here: if (singleuserdata.size() == 1)
seem checking size of 1 element. within same scope, accessing multiple items.
you need change check: if (singleuserdata.size() == 1)
if (singleuserdata.size() == 5)
.
Comments
Post a Comment