Saturday 15 January 2011

java - NullPointerException in SQLite database at line 71 -



java - NullPointerException in SQLite database at line 71 -

i developing application whereby storing , retrieving info database. info retrieved randomly choosing id ('id' primary key constrained column in table), storing in cursor object , displaying along radio buttons. 1 of these radio buttons selected user , accordingly result displayed.

i have implemented (code : dbhelperdisplay.java) :

package course.examples.jumboquest; import java.util.arraylist; import java.util.random; //import java.io.*; //import java.lang.*; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.view; import android.widget.button; //import android.widget.edittext; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.textview; //import android.content.intent; import android.os.countdowntimer; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.databaseutils; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitedatabase; public class dbhelperdisplay extends actionbaractivity { textview tv; dbhelper mydb; radiogroup radiochoices; radiobutton rbtchoice; button btsubmit; string choice1; string choice2; string choice3; string choice4; string strans; customtimer cdt; textview quest; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.dbhelper_display); cdt = new customtimer(20000, 1000); cdt.start(); mydb = new dbhelper(this); mydb.insertquestion(1, "who team fellow member name starts s?", "vinita", "akanksha", "swati", "megha", "swati"); mydb.insertquestion(2, "who team fellow member name starts m?", "vinita", "akanksha", "swati", "megha", "megha"); mydb.insertquestion(3, "who team fellow member name starts a?", "vinita", "akanksha", "swati", "megha", "akanksha"); mydb.insertquestion(4, "who team fellow member name starts v?", "vinita", "akanksha", "swati", "megha", "vinita"); mydb.insertquestion(5, "who team fellow member name ends i?", "vinita", "akanksha", "swati", "megha", "swati"); cursor rs = mydb.getdata(); rs.movetofirst(); string question = rs.getstring(rs.getcolumnindex(dbhelper.col_ques)); choice1 = rs.getstring(rs.getcolumnindex(dbhelper.col_choice1)); choice2 = rs.getstring(rs.getcolumnindex(dbhelper.col_choice2)); choice3 = rs.getstring(rs.getcolumnindex(dbhelper.col_choice3)); choice4 = rs.getstring(rs.getcolumnindex(dbhelper.col_choice4)); strans = rs.getstring(rs.getcolumnindex(dbhelper.col_ans)); tv = (textview) findviewbyid(r.id.timertxt); quest = (textview) findviewbyid(r.id.quest); quest.settext(question); //final textview ans = (textview) findviewbyid(r.id.ans); button btclear = (button)findviewbyid(r.id.btclear); btclear.settext("clear"); // line 71 addlistenerradiochoices() ; btclear.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { //ans.settext(""); } }); } public void addlistenerradiochoices(){ radiochoices = (radiogroup) findviewbyid(r.id.radiochoices); ((radiobutton) radiochoices.getchildat(0)).settext(choice1); ((radiobutton) radiochoices.getchildat(1)).settext(choice2); ((radiobutton) radiochoices.getchildat(2)).settext(choice3); ((radiobutton) radiochoices.getchildat(3)).settext(choice4); btsubmit = (button)findviewbyid(r.id.btsubmit); btsubmit.settext("submit"); btsubmit.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { int selected = radiochoices.getcheckedradiobuttonid(); rbtchoice = (radiobutton) findviewbyid(selected); string ans = rbtchoice.gettext().tostring(); if(ans.equalsignorecase(strans)){ cdt.cancel(); //ans.settext(" answer"); } } }); } public class customtimer extends countdowntimer{ //textview ed; public customtimer(long millisinfuture, long countdowninterval){ super(millisinfuture, countdowninterval); } @override public void ontick(long millisuntilfinished){ //current = millisuntilfinished/1000; tv.settext("time left:" + millisuntilfinished/1000); } @override public void onfinish() { tv.settext("time - lost game!"); } } public class dbhelper extends sqliteopenhelper { public static final string database_name = "questions.db"; public static final string table_name = "comics"; public static final string col_id = "id"; public static final string col_ques = "question"; public static final string col_choice1 = "choice1"; public static final string col_choice2 = "choice2"; public static final string col_choice3 = "choice3"; public static final string col_choice4 = "choice4"; public static final string col_ans = "answer"; public dbhelper(context context){ super(context, database_name, null, 4); } @override public void oncreate(sqlitedatabase db){ // todo auto-generated method stub string create_table = "create table " + table_name + "( " + col_id + " integer primary key, " + col_ques + " text, " + col_choice1 + " text, " + col_choice2 + " text, " + col_choice3 + " text, " + col_choice4 + " text, " + col_ans + " 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 comics"); oncreate(db); } public boolean insertquestion(int id, string question, string choice1, string choice2, string choice3, string choice4, string answer){ sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put("id",id); contentvalues.put("question",question); contentvalues.put("choice1",choice1); contentvalues.put("choice2",choice2); contentvalues.put("choice3",choice3); contentvalues.put("choice4",choice4); contentvalues.put("answer",answer); db.insert("comics", null, contentvalues); db.close(); homecoming true; } public cursor getdata(){ //public void getdata(){ sqlitedatabase db = this.getreadabledatabase(); int numrows = (int)databaseutils.querynumentries(db,table_name); //system.out.println(numrows); int min = 1; int max = numrows; random r = new random(); int id = r.nextint(max - min + 1) + min; // cursor res= db.rawquery("select * comics", null); // arraylist<string> array_list = new arraylist<string>(); cursor res= db.rawquery("select * comics id = '" + id + "'", null); /*res.movetofirst(); while(res.isafterlast() == false){ array_list.add(res.getstring(res.getcolumnindex(col_id))); res.movetonext(); } //quest.settext(""); stringbuilder text = new stringbuilder(); for(int i= 0; i<array_list.size();i++){ text.append(array_list.get(i).tostring() + "\n"); } quest.settext(text.tostring());*/ homecoming res; } } }

my dbhelper_display.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/timertxt" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/quest" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <radiogroup android:id="@+id/radiochoices" android:layout_width="wrap_content" android:layout_height="wrap_content" > <radiobutton android:id="@+id/rbtchoice1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <radiobutton android:id="@+id/rbtchoice2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <radiobutton android:id="@+id/rbtchoice3" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <radiobutton android:id="@+id/rbtchoice4" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </radiogroup> <button android:id="@+id/btnsubmit" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <button android:id="@+id/btnclear" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/ans" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>

i getting nullpointerexception on implementing above code. have attached logcat same.

log cat result :

10-24 07:57:44.567: d/dalvikvm(1498): gc_for_alloc freed 91k, 5% free 3265k/3432k, paused 80ms, total 81ms 10-24 07:57:45.107: d/gralloc_goldfish(1498): emulator without gpu emulation detected. 10-24 07:57:51.387: d/androidruntime(1498): shutting downwards vm 10-24 07:57:51.387: w/dalvikvm(1498): threadid=1: thread exiting uncaught exception (group=0xb1a47ba8) 10-24 07:57:51.437: e/androidruntime(1498): fatal exception: main 10-24 07:57:51.437: e/androidruntime(1498): process: course.examples.jumboquest, pid: 1498 10-24 07:57:51.437: e/androidruntime(1498): java.lang.runtimeexception: unable start activity componentinfo{course.examples.jumboquest/course.examples.jumboquest.dbhelperdisplay}: java.lang.nullpointerexception 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.activitythread.access$800(activitythread.java:135) 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 10-24 07:57:51.437: e/androidruntime(1498): @ android.os.handler.dispatchmessage(handler.java:102) 10-24 07:57:51.437: e/androidruntime(1498): @ android.os.looper.loop(looper.java:136) 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.activitythread.main(activitythread.java:5017) 10-24 07:57:51.437: e/androidruntime(1498): @ java.lang.reflect.method.invokenative(native method) 10-24 07:57:51.437: e/androidruntime(1498): @ java.lang.reflect.method.invoke(method.java:515) 10-24 07:57:51.437: e/androidruntime(1498): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 10-24 07:57:51.437: e/androidruntime(1498): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 10-24 07:57:51.437: e/androidruntime(1498): @ dalvik.system.nativestart.main(native method) 10-24 07:57:51.437: e/androidruntime(1498): caused by: java.lang.nullpointerexception 10-24 07:57:51.437: e/androidruntime(1498): @ course.examples.jumboquest.dbhelperdisplay.oncreate(dbhelperdisplay.java:71) 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.activity.performcreate(activity.java:5231) 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 10-24 07:57:51.437: e/androidruntime(1498): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 10-24 07:57:51.437: e/androidruntime(1498): ... 11 more 10-24 07:57:55.767: i/process(1498): sending signal. pid: 1498 sig: 9

what error in implementing database ? also, before showing error 'cursor index requested -1 size 1'. did changes , showing above error. should done in previous case if error comes again? please help me. new sqlite databases in android.

i suspect typo, not have button android:id="@+id/btclear within of dbhelper_display.xml layout file. based on fact able compile, suspect have id in different file, not in dbhelper_display.xml.

from posted xml code, see this:

android:id="@+id/btnclear"

the java code says this:

button btclear = (button)findviewbyid(r.id.btclear);

make these consistent, , work. check btnsubmit, believe has same issue.

java android sqlite nullpointerexception android-xml

No comments:

Post a Comment