我有一个Helper类,它扩展了SQLiteOpenHelper,因为我有四个方法
onCreate(SQLiteDatabase sqLiteDatabase)
onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
addquestions()
addingeachquestions(Questions question)// I also have one class named as Questions
我将SQLiteDatabase
对象声明为" db"在我的Helperclass下
但是当我在我的onCreate&中做"db=sqLiteDatabase"
时onUpgrade方法它工作正常,但是当我不在我的"db=sqLiteDatabase"
方法中生成语句onCreate & onUpgrade
时,logcat
窗口在行处显示空指针异常
"db.insert(Helperclass.TABLE_NAME, null, values);"
这是我的代码
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);
}
答案 0 :(得分:2)
无需在db = sqLiteDatabase;
中添加onCreate
行。在getWritableDatabase
方法调用db
中调用insert
方法之前调用addingeachquestions
方法初始化getWritableDatabase()
对象,然后再使用它。
db=getWritableDatabase();
db.insert(Helperclass.TABLE_NAME, null, values);
答案 1 :(得分:0)
你必须在“addingeachquestions”方法中得到Db的对象,如下所示:
SQLiteDatabase db = this.getWritableDatabase();
或
db = this.getWritableDatabase();
答案 2 :(得分:0)
在调用助手类时初始化db: -
Helperclass helperClassObj = new Helperclass(context);
db = helperClassObj.getWritableDatabase();
答案 3 :(得分:0)
这是因为SQLiteDatabase
,db
的实例为NULL。
您完全不需要在onCretae(SQLiteDatabase sqLiteDatabase)和db = sqLiteDatabase;
中添加onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
,而是使用以下代码。
if (db ==null){
db = this.getWritableDatabase();
}