Reputation: 173
I am trying to insert the data in SQLITE database but my application is crashing when I am trying to do so here is my code I am trying to insert data on Final click or radio button. This is the insert block
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// Log.d("Questions", "Moving to next question");
a++;
/**
* validate a checkbox has been selected
*/
if (!checkAnswer())
return;
/**
* check if end of game
*/
if (currentGame.isGameOver()) {
db.open();
String total = currentGame.getRight() + "";
Log.i("Sanket", total);
db.insertOptions(topic1, total, mon);
db.close();
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
a = 0;
finish();
} else {
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
}
Here is my DatabaseAdapter Class I have defined all methods here
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class DBAdapter {
static final String topic = "topic";
static final String score = "score";
static final String month = "month";
static final String DATABASE_NAME = "questionDb";
static final String DATABASE_TABLE = "marks";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE = "create table marks"
+ "(topic text,score text,month text);";
final Context context;
DatabseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context context) {
this.context = context;
DBHelper = new DatabseHelper(context);
}
private static class DatabseHelper extends SQLiteOpenHelper {
public DatabseHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.i("Sanket", "Upgrading database version from versin "
+ oldVersion + " to " + newVersion
+ " which may cause to destroy all old data");
db.execSQL("DROP TABLE IF EXISTS marks");
onCreate(db);
}
}
public DBAdapter open() {
// TODO Auto-generated method stub
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
// TODO Auto-generated method stub
DBHelper.close();
}
public long insertOptions(String topic1, String mon,
String total) {
// TODO Auto-generated method stub
ContentValues initialValues = new ContentValues();
initialValues.put(topic, topic1);
initialValues.put(score, mon);
initialValues.put(month, total);
return db.insert(DATABASE_TABLE, null, initialValues);
}
Cursor getAllQuestions() {
// TODO Auto-generated method stub
return db.query(DATABASE_TABLE, new String[] { topic, score, month },
null, null, null, null, null);
}
long getNoOfRows() {
String sql = "SELECT COUNT(*) FROM " + DATABASE_TABLE;
SQLiteStatement statement = db.compileStatement(sql);
long count = statement.simpleQueryForLong();
return count;
}
Cursor getAlldata() {
String selectQuery = ("select * from marks");
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
}
Please go through my code and let me know the required changes
here is the LOGCAT
02-04 13:06:57.259: E/AndroidRuntime(1114): FATAL EXCEPTION: main 02-04 13:06:57.259: E/AndroidRuntime(1114): java.lang.NullPointerException: println needs a message 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.util.Log.println_native(Native Method) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.util.Log.i(Log.java:159) 02-04 13:06:57.259: E/AndroidRuntime(1114): at com.tmm.android.chuck.QuestionActivity.onCheckedChanged(QuestionActivity.java:195) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:174) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.widget.RadioGroup.access$600(RadioGroup.java:54) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:358) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.widget.CompoundButton.setChecked(CompoundButton.java:129) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.widget.CompoundButton.toggle(CompoundButton.java:87) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.widget.RadioButton.toggle(RadioButton.java:76) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.widget.CompoundButton.performClick(CompoundButton.java:99) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.view.View$PerformClick.run(View.java:17721) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.os.Handler.handleCallback(Handler.java:730) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.os.Handler.dispatchMessage(Handler.java:92) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.os.Looper.loop(Looper.java:137) 02-04 13:06:57.259: E/AndroidRuntime(1114): at android.app.ActivityThread.main(ActivityThread.java:5103) 02-04 13:06:57.259: E/AndroidRuntime(1114): at java.lang.reflect.Method.invokeNative(Native Method) 02-04 13:06:57.259: E/AndroidRuntime(1114): at java.lang.reflect.Method.invoke(Method.java:525) 02-04 13:06:57.259: E/AndroidRuntime(1114): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 02-04 13:06:57.259: E/AndroidRuntime(1114): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 02-04 13:06:57.259: E/AndroidRuntime(1114): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 918
Reputation: 152797
Your database handler onCreate()
is empty. Your database doesn't have any tables.
Put the required CREATE TABLE
SQL there and uninstall your app so the old, empty database file is removed.
The exception stacktrace "println needs a message" you added to your question comes from here:
String total = currentGame.getRight() + "";
Log.i("Sanket", total);
because total
is an empty string. Change the logging to something like
String total = currentGame.getRight() + "";
Log.i("Sanket", "total: " + total);
Upvotes: 1
Reputation: 1329
You haven't created marks
table in your DBAdapter
class
create table
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(TABLE_NAME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// create new tables
onCreate(db);
}
Upvotes: 3