Reputation: 33
I have a Helper class which extends SQLiteOpenHelper in that i have four method
onCreate(SQLiteDatabase sqLiteDatabase)
onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
addquestions()
addingeachquestions(Questions question)// I also have one class named as Questions
I declared a SQLiteDatabase
object as "db" under my Helperclass
but when i make "db=sqLiteDatabase"
in my onCreate & onUpgrade method it works fine but when i don't make statement "db=sqLiteDatabase"
in my onCreate & onUpgrade
method then logcat
windows says null pointer exception at line
"db.insert(Helperclass.TABLE_NAME, null, values);"
Here is my code
Helperclass.java
public class Helperclass extends SQLiteOpenHelper {
SQLiteDatabase db;
Context context;
Helperclass helperclass;
private static final String DATABASE_NAME = "DATABASE1";
private static final int DATABASE_VERSION = 52;
private static final String TABLE_NAME = "TABLE1";
private static final String UID = "_ID";
private static final String QUESTION = "QUESTION";
private static final String OPT1 = "OPT1";
private static final String OPT2 = "OPT2";
private static final String OPT3 = "OPT3";
private static final String ANSWER = "ANSWER";
private static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY, " + QUESTION + " VARCHAR(255), " + OPT1 + " VARCHAR(255), " + OPT2 + " VARCHAR(255), " + OPT3 + " VARCHAR(255), " + ANSWER + " VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
public Helperclass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Toast.makeText(context, "constructor called", Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
db = sqLiteDatabase; //when i mentions this it works fine but when i don't it gives error. Why i have to mention this?
sqLiteDatabase.execSQL(CREATE_TABLE);
addquestions();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
db = sqLiteDatabase; //when i mentions this it works fine but when i don't it gives error.
sqLiteDatabase.execSQL(DROP_TABLE);
onCreate(sqLiteDatabase);
}
public void addquestions() {
Questions q1 = new Questions("what is your hobby?", "singing", "dancing", "reading", "sining");
this.addingeachquestions(q1);
Questions q2 = new Questions("how are you?", "fine", "good", "sad", "good");
this.addingeachquestions(q2);
Questions q3 = new Questions("whats your name?", "sam", "jhon", "alice", "sam");
this.addingeachquestions(q3);
Toast.makeText(context, "addquestion method called", Toast.LENGTH_SHORT).show();
}
private void addingeachquestions(Questions question) {
ContentValues values = new ContentValues();
values.put(Helperclass.QUESTION, question.getQUESTION());
values.put(Helperclass.OPT1, question.getOPTA());
values.put(Helperclass.OPT2, question.getOPTB());
values.put(Helperclass.OPT3, question.getOPTC());
values.put(Helperclass.ANSWER, question.getANSWER());
db.insert(Helperclass.TABLE_NAME, null, values);
}
Upvotes: 0
Views: 627
Reputation: 719
It is because instance of SQLiteDatabase
, db
is NULL.
You have absolutely no need to add db = sqLiteDatabase;
in onCretae(SQLiteDatabase sqLiteDatabase) and onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
instead use the following code.
if (db ==null){
db = this.getWritableDatabase();
}
Upvotes: 0
Reputation: 417
Initialize db while calling your helper class:-
Helperclass helperClassObj = new Helperclass(context);
db = helperClassObj.getWritableDatabase();
Upvotes: 0
Reputation: 2877
You have to get object of Db in "addingeachquestions" method like this :
SQLiteDatabase db = this.getWritableDatabase();
or
db = this.getWritableDatabase();
Upvotes: 0
Reputation: 132982
No need to add db = sqLiteDatabase;
line in onCreate
. call getWritableDatabase
method to initialize db
object before using it.like before calling insert
method in addingeachquestions
method call getWritableDatabase()
:
db=getWritableDatabase();
db.insert(Helperclass.TABLE_NAME, null, values);
Upvotes: 2