Issue
I am developing an application whereby I am storing and retrieving data from database. The data is retrieved by randomly choosing id ('id' is the primary key constrained column in the table), storing in cursor object and displaying along with radio buttons. One of these Radio Buttons is then selected by the user and accordingly the result is displayed.
I have implemented this as (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 is the team member whose name starts with s?", "Vinita", "Akanksha", "Swati", "Megha", "Swati");
myDB.insertQuestion(2, "who is the team member whose name starts with m?", "Vinita", "Akanksha", "Swati", "Megha", "Megha");
myDB.insertQuestion(3, "who is the team member whose name starts with a?", "Vinita", "Akanksha", "Swati", "Megha", "Akanksha");
myDB.insertQuestion(4, "who is the team member whose name starts with v?", "Vinita", "Akanksha", "Swati", "Megha", "Vinita");
myDB.insertQuestion(5, "who is the team member whose name ends with 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 Up - lost the 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();
return 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 * from Comics", null);
// ArrayList<String> array_list = new ArrayList<String>();
Cursor res= db.rawQuery("Select * from Comics where 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());*/
return 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 am getting NullPointerException on implementing the above code. I have attached the LOgCat for the 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 down VM
10-24 07:57:51.387: W/dalvikvm(1498): threadid=1: thread exiting with 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 to start activity ComponentInfo{course.examples.jumboquest/course.examples.jumboquest.DBHelperDisplay}: java.lang.NullPointerException
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.os.Handler.dispatchMessage(Handler.java:102)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.os.Looper.loop(Looper.java:136)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-24 07:57:51.437: E/AndroidRuntime(1498): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 07:57:51.437: E/AndroidRuntime(1498): at java.lang.reflect.Method.invoke(Method.java:515)
10-24 07:57:51.437: E/AndroidRuntime(1498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-24 07:57:51.437: E/AndroidRuntime(1498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-24 07:57:51.437: E/AndroidRuntime(1498): at 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): at course.examples.jumboquest.DBHelperDisplay.onCreate(DBHelperDisplay.java:71)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.Activity.performCreate(Activity.java:5231)
10-24 07:57:51.437: E/AndroidRuntime(1498): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-24 07:57:51.437: E/AndroidRuntime(1498): at 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 is the error in implementing database? Also, earlier it was showing the error 'cursor index requested -1 with size 1'. I did some changes and now it is showing the above error. What should be done in the previous case too if that error comes again? I am new to SQLite databases in Android.
Solution
I strongly suspect this is a typo, that you do not have a Button with an android:id="@+id/btClear
inside of your dbhelper_display.xml layout file. Based on the fact that you were able to compile, I suspect that you have that ID in a different file, but not in dbhelper_display.xml.
From your posted XML code, I see this:
android:id="@+id/btnClear"
The Java Code says this:
Button btClear = (Button)findViewById(R.id.btClear);
Make these consistent, and it will work. Also check your btnSubmit
, which I believe has the same issue.
Answered By - PearsonArtPhoto
Answer Checked By - Pedro (JavaFixing Volunteer)