i'm trying insert , retrieve data database in eclipse using sqlite, shows runtimeerror
. create layout 3 edit texts , 1 button create simple information nothing created. create java database following code:
package com.example.databasetest; import android.content.contentvalues; import android.content.context; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class dbhelper extends sqliteopenhelper{ private static final int database_version = 1; private static final string database_name = "teacher"; private static final string table_name = "teacher_table"; private static final string name = "teacher_name"; private static final string father_name = "father_name"; private static final string mother_name = "mother_name"; sqlitedatabase data=this.getwritabledatabase(); context ctx; public dbhelper(context context) { super(context, database_name, null, database_version); ctx=context; log.d("database operation", "database created"); } @override public void oncreate(sqlitedatabase db) { data=db; db.execsql("create table " + table_name + " (" + name + " text," + father_name + " text," + mother_name + " integer," + ");"); log.d("database operation", "table created"); } public void open() throws sqlexception { dbhelper db1 = new dbhelper(ctx); data = db1.getwritabledatabase(); } public void close() { data.close(); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exception exists"); oncreate(db); } public void oninsert(dbhelper db,string name,string f_name, string m_name) { sqlitedatabase sql= db.getwritabledatabase(); contentvalues cv=new contentvalues(); cv.put("teacher_name",name); cv.put("father_name", f_name); cv.put("mother_name", m_name); sql.insert(table_name, null, cv); log.d("database operation", "one row inserted....."); } }
and java file as...
package com.example.databasetest; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class mainactivity extends activity { edittext name,father,mother,id; string name,father,mother,id; int i=1; button save; context ctxx; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); name=(edittext)findviewbyid(r.id.name); father=(edittext)findviewbyid(r.id.father_name); mother=(edittext)findviewbyid(r.id.mother_name); id=(edittext)findviewbyid(r.id.emp_id); save=(button)findviewbyid(r.id.submit); save.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { id=id.gettext().tostring(); name=name.gettext().tostring(); father=father.gettext().tostring(); mother=mother.gettext().tostring(); if(id.equals(i)) { toast.maketext(getbasecontext(), "not allow insertion", toast.length_long).show(); } else { dbhelper db=new dbhelper(ctxx); db.open(); db.oninsert(db, name, father, mother); toast.maketext(getbasecontext(), "insertion sucessful", toast.length_long).show(); finish(); } } }); } }
and show run time error when click on button in log cat..
05-08 02:54:05.932: d/androidruntime(922): shutting down vm 05-08 02:54:05.999: w/dalvikvm(922): threadid=1: thread exiting uncaught exception (group=0x41465700) 05-08 02:54:06.189: e/androidruntime(922): fatal exception: main 05-08 02:54:06.189: e/androidruntime(922): java.lang.nullpointerexception 05-08 02:54:06.189: e/androidruntime(922): @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:224) 05-08 02:54:06.189: e/androidruntime(922): @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:164) 05-08 02:54:06.189: e/androidruntime(922): @ com.example.databasetest.dbhelper.<init>(dbhelper.java:22) 05-08 02:54:06.189: e/androidruntime(922): @ com.example.databasetest.mainactivity$1.onclick(mainactivity.java:49) 05-08 02:54:06.189: e/androidruntime(922): @ android.view.view.performclick(view.java:4240) 05-08 02:54:06.189: e/androidruntime(922): @ android.view.view$performclick.run(view.java:17721) 05-08 02:54:06.189: e/androidruntime(922): @ android.os.handler.handlecallback(handler.java:730) 05-08 02:54:06.189: e/androidruntime(922): @ android.os.handler.dispatchmessage(handler.java:92) 05-08 02:54:06.189: e/androidruntime(922): @ android.os.looper.loop(looper.java:137) 05-08 02:54:06.189: e/androidruntime(922): @ android.app.activitythread.main(activitythread.java:5103) 05-08 02:54:06.189: e/androidruntime(922): @ java.lang.reflect.method.invokenative(native method) 05-08 02:54:06.189: e/androidruntime(922): @ java.lang.reflect.method.invoke(method.java:525) 05-08 02:54:06.189: e/androidruntime(922): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 05-08 02:54:06.189: e/androidruntime(922): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 05-08 02:54:06.189: e/androidruntime(922): @ dalvik.system.nativestart.main(native method) 05-08 02:54:09.589: i/process(922): sending signal. pid: 922 sig: 9
ctxx
never initialzed, , cause of crash. speaking, when deal activity , fragment subclass, never need keep reference context. activity subclass of context
, , this
enough. in fragment can retrieve context of activity
hosting fragment getactivity()
chante
dbhelper db=new dbhelper(ctxx);
with
dbhelper db=new dbhelper(mainactivity.this);
as @dergolem pointed out, using type integer column mother_name
. want use text
, instead, , need primary key "_id"
db.execsql("create table " + table_name + " (" + basecolumns._id + " integer primary key autoincrement, " + name + " text," + father_name + " text," + mother_name + " text" + ");");
Comments
Post a Comment