java - ERROR in sqlite database connectivity -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
kindly me resolve error. getting error in android studio. new android development. problem registration..not able insert data database. error is:
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.cursor android.database.sqlite.sqlitedatabase.rawquery(java.lang.string, java.lang.string[])' on null object reference
db_connectivity
package com.freedomkitchen.sonali.freedomkitchenandroidapp; import android.annotation.suppresslint; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.support.v7.app.actionbar; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.toast; import com.freedomkitchen.sonali.freedomkitchenandroidapp.newuserwelcome; import java.util.logging.handler; import java.util.logging.logrecord; public class db_connectivity extends appcompatactivity { sqlitedatabase mydb; int user_id; private static final boolean auto_hide = true; private static final int auto_hide_delay_millis = 3000; private static final int ui_animation_delay = 300; private final handler mhidehandler = new handler() { @override public void close() { } @override public void flush() { } @override public void publish(logrecord record) { } }; private view mcontentview; private final runnable mhidepart2runnable = new runnable() { @suppresslint("inlinedapi") @override public void run() { mcontentview.setsystemuivisibility(view.system_ui_flag_low_profile | view.system_ui_flag_fullscreen | view.system_ui_flag_layout_stable | view.system_ui_flag_immersive_sticky | view.system_ui_flag_layout_hide_navigation | view.system_ui_flag_hide_navigation); } }; private view mcontrolsview; private final runnable mshowpart2runnable = new runnable() { @override public void run() { // delayed display of ui elements actionbar actionbar = getsupportactionbar(); if (actionbar != null) { actionbar.show(); } mcontrolsview.setvisibility(view.visible); } }; private boolean mvisible; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mydb = openorcreatedatabase("freedomkitchendb.db", mode_private, null); mydb.execsql("create table if not exists user_details(user_id integer primary key," + "first_name text,last_name text,gender text ,age text,country text);"); mydb.execsql("create table if not exists login_details(user_id integer," + "username text,password text,foreign key(user_id) references user_details(user_id));"); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); } public void userlogin(string username,string password) { mydb.execsql("select user_id login_details username=" + username + "and password=" + password+");"); cursor cursor_one = mydb.rawquery("select * user_details", null); int rows = cursor_one.getcount(); cursor_one.movetofirst(); if (rows == 0) { toast.maketext(getapplicationcontext(), "incorrect username or password. please try again.", toast.length_short).show(); } else if (rows == 1) { intent = new intent(getapplicationcontext(), freedomkitchenmenu.class); startactivity(i); } } public void registration(string first_name,string last_name,string age,string gender,string country,string username,string password){ int user_id=0; cursor cursor_one = mydb.rawquery("select * user_details", null); int rows = cursor_one.getcount(); cursor_one.movetofirst(); user_id=rows+1; mydb.execsql("insert user_details(user_id,user_firstname,user_lastname,gender,age,country)values("+user_id+"','"+first_name+"','" + last_name + "','" + gender + "','" + age + "','" + country + ");"); mydb.execsql("insert login_details(user_id,username,password)values(" + user_id + "','" + username+ "','" + password + ");"); intent i=new intent(getapplicationcontext(),newuserwelcome.class); startactivity(i); } }
logcat
04-22 07:58:57.111 31243-31243/? e/zygote: v2 04-22 07:58:57.121 31243-31243/? e/selinux: [debug] get_category: variable seinfo: default sensitivity: null, cateogry: null 04-22 07:59:08.501 31243-31243/com.freedomkitchen.sonali.freedomkitchen e/viewrootimpl: senduseractionevent() mview == null 04-22 07:59:12.971 31243-31243/com.freedomkitchen.sonali.freedomkitchen e/viewrootimpl: senduseractionevent() mview == null 04-22 07:59:33.221 31243-31243/com.freedomkitchen.sonali.freedomkitchen e/androidruntime: fatal exception: main process: com.freedomkitchen.sonali.freedomkitchen, pid: 31243 java.lang.illegalstateexception: not execute method android:onclick @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:293) @ android.view.view.performclick(view.java:5076) @ android.view.view$performclick.run(view.java:20279) @ 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:5910) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1405) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1200) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288) @ android.view.view.performclick(view.java:5076) @ android.view.view$performclick.run(view.java:20279) @ 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:5910) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1405) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1200) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.cursor android.database.sqlite.sqlitedatabase.rawquery(java.lang.string, java.lang.string[])' on null object reference @ com.freedomkitchen.sonali.freedomkitchenandroidapp.db_connectivity.registration(db_connectivity.java:122) @ com.freedomkitchen.sonali.freedomkitchenandroidapp.registration.reg(registration.java:93) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288) @ android.view.view.performclick(view.java:5076) @ android.view.view$performclick.run(view.java:20279) @ 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:5910) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1405) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1200)
on click
public void reg(view v){ spinner gender,country; edittext fname,lname,username,password,age; fname = (edittext)findviewbyid(r.id.firstname_et); lname = (edittext)findviewbyid(r.id.lastname_et); gender=(spinner)findviewbyid(r.id.spinnergender); age=(edittext)findviewbyid(r.id.age_et); country=(spinner)findviewbyid(r.id.spinnercountry); password =(edittext)findviewbyid(r.id.password_et); username =(edittext)findviewbyid(r.id.username_et); first_name=fname.gettext().tostring(); last_name=lname.gettext().tostring(); age=age.gettext().tostring(); user_name=username.gettext().tostring(); pass_word=password.gettext().tostring(); gender=gender.getselecteditem().tostring(); country=country.getselecteditem().tostring(); db_connectivity mydb=new db_connectivity(); mydb.registration(first_name,last_name,age,gender,country,user_name,pass_word); }
you have code below
public class dbhelper extends sqliteopenhelper { public static final string database_name = "freedomkitchendb.db"; private static final int database_version =1; //********** login table columns ************* public static final string login_table_name = "login_details"; public static final string login_column_id = "user_id"; public static final string login_column_username = "username"; public static final string login_column_password = "password"; //********** registration table columns ************* public static final string reg_table_name = "user_details"; public static final string reg_column_id = "user_id"; public static final string reg_column_first_name = "first_name"; public static final string reg_column_last_name = "last_name"; public static final string reg_column_gender = "gender"; public static final string reg_column_age = "age"; public static final string reg_column_country = "country"; public dbhelper(context context) { super(context, database_name, null,database_version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub string create_reg_table="create table " +reg_table_name+"("+reg_column_id+" integer primary key autoincrement,"+reg_column_first_name+" text,"+reg_column_last_name+" text,"+reg_column_gender+" text,"+reg_column_age+" text,"+reg_column_country+" text)"; db.execsql(create_reg_table); string create_login_table="create table " + login_table_name + "(" + login_column_id + " integer," + login_column_username + " text," + login_column_password + " text,foreign key(" + login_column_id + ") references " + reg_table_name + "(" + reg_column_id + "))"; db.execsql(create_login_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists contacts"); oncreate(db); } public boolean insertuserdetail (string first_name,string last_name,string age,string gender,string country,string username,string password) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(reg_column_first_name, first_name); contentvalues.put(reg_column_last_name, last_name); contentvalues.put(reg_column_gender, gender); contentvalues.put(reg_column_age, age); contentvalues.put(reg_column_country, country); contentvalues mcontentvalues1=new contentvalues(); mcontentvalues1.put(login_column_username,username); mcontentvalues1.put(login_column_password,password); db.insert(login_table_name, null, mcontentvalues1); db.insert(reg_table_name, null, contentvalues); return true; } public boolean login(string username, string password) throws sqlexception { sqlitedatabase db = this.getreadabledatabase(); cursor mcursor = db.rawquery("select * " + login_table_name + " "+login_column_username+"=? , "+login_column_password+"=?", new string[]{username,password}); if (mcursor != null) { if(mcursor.getcount() > 0) { return true; } } return false; }}
Comments
Post a Comment