Unable to insert data in the table residing in SQLite database in my android app -


kindly note designing app involves creation of database on running first time , opening in runs follow.so have handled following way in 'mainactiviy' class in 'oncreate()' method:

try  {     mydatabase=this.openorcreatedatabase("wishes", mode_private, null);     mydatabase.execsql("create table if not exists " + tablename                  +                 " (_id varchar(15) primary key,name varchar(20), message varchar(100),event varchar(15),date int);");  }    //sqlitedatabase 'mydatabase' declared public 

now have class called 'eventdefinition' wherein create 'mainactivity' object 'ma' , try insert values in table on button click event follows:

              mainactivity ma=new mainactivity();     //line 63               event=spinner.getselecteditem().tostring();   //line 64               message=ed1.gettext().tostring();    //line 65               ma.mydatabase.opendatabase("/data/data/org.mithil.autowish/databases/wishes", null,ma.mydatabase.open_readwrite);   //line 66               ma.mydatabase.execsql("insert " +                                 ma.tablename +                         " values ('"+ finalnumber +"','"+ name +"','"+ message +"','"+ event +"','"+ ldate +"');"); 

the code giving me nullpointerexception follows:

05-10 11:03:30.992: e/androidruntime(602): fatal exception: main 05-10 11:03:30.992: e/androidruntime(602): java.lang.nullpointerexception 05-10 11:03:30.992: e/androidruntime(602): @ org.mithil.autowish.eventdefinition$2.onclick(eventdefinition.java:67) 05-10 11:03:30.992: e/androidruntime(602): @ android.view.view.performclick(view.java:3480) 05-10 11:03:30.992: e/androidruntime(602): @ android.view.view$performclick.run(view.java:13983) 05-10 11:03:30.992: e/androidruntime(602): @ android.os.handler.handlecallback(handler.java:605) 05-10 11:03:30.992: e/androidruntime(602): @ android.os.handler.dispatchmessage(handler.java:92) 05-10 11:03:30.992: e/androidruntime(602): @ android.os.looper.loop(looper.java:137) 05-10 11:03:30.992: e/androidruntime(602): @ android.app.activitythread.main(activitythread.java:4340) 05-10 11:03:30.992: e/androidruntime(602): @ java.lang.reflect.method.invokenative(native method) 05-10 11:03:30.992: e/androidruntime(602): @ java.lang.reflect.method.invoke(method.java:511) 05-10 11:03:30.992: e/androidruntime(602): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 05-10 11:03:30.992: e/androidruntime(602): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 05-10 11:03:30.992: e/androidruntime(602): @ dalvik.system.nativestart.main(native method)

please let me know doing wrong.any assistance mean lot.thanks in advance.

regards

you need separate out activity. easiest method implement single dao extends sqliteopenhelper class.

java doc here http://developer.android.com/reference/android/database/sqlite/sqliteopenhelper.html

this class has built in logic create new database or update database based on version don't have re-implement logic in activity database creation.

all need implement oncreate(db) method , onupdate(db, oldver, newver) methods.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -