我有一个sql数据库,我认为该数据库可以保存学生的姓名和电话号码。我可以轻松地将项目添加到数据库,并可以轻松列出它们。但是,当我想通过其ID调用特定项目时,出现以下错误:
java.lang.NullPointerException:尝试调用虚拟方法'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String,int,android.database.sqlite.SQLiteDatabase $ CursorFactory ,android.database.DatabaseErrorHandler)”上的空对象引用
我的模拟器指向该行:
SQLiteDatabase db = this.getReadableDatabase();
部分:getId(int id)
public StudentsinLib getId(int id){
String selectQuery = "SELECT * FROM " + STUDENTDATABASETABLE_NAME + " WHERE "+ COL + " ="+id;
SQLiteDatabase db = this.getReadableDatabase(); // ======> THE EMULATOR POINTS HERE
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null)
cursor.moveToFirst();
StudentsinLib studentsinLib = new StudentsinLib();
studentsinLib.setId(cursor.getInt(0));
studentsinLib.setName(cursor.getString(1));
studentsinLib.setNumber(cursor.getString(2));
//log
Log.d("getStudent(" + id + ")", StudentsinLib.toString());
// 5. return student
return studentsinLib;
}
我在SQLiteDatabase中的全部代码
public class StudentsinLibDatabase extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String STUDENTDATABASE_NAME = "StudentsinLibDB";
// Table Content
private static final String STUDENTDATABASETABLE_NAME = "StudentsinLibTB";
private static String COL = "studentsinLibId";
private static String COL1 = "StudentsinLibName";
private static String COL2 = "studentsinLibNumber";
private static final String[] COLUMNS = {COL,COL1,COL2};
public Context context;
public StudentsinLibDatabase(Context context) {
super(context, STUDENTDATABASETABLE_NAME, null, DATABASE_VERSION);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE "+STUDENTDATABASETABLE_NAME+"("
+COL+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+COL1+" TEXT,"
+COL2+" TEXT);";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older students table if existed
db.execSQL("DROP TABLE IF EXISTS students");
// create fresh students table
this.onCreate(db);
}
public void addStudentstoLib(StudentsinLib studentstoLib){
Log.d("addStudent", studentstolib.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(COL, studentstolib.getId());
values.put(COL1, studentstolib.getName());
values.put(COL2, studentstolib.getNumber());
// 3. insert
db.insert(STUDENTDATABASETABLE_NAME, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
public StudentsinLib getId(int id){
String selectQuery = "SELECT * FROM " + STUDENTDATABASETABLE_NAME + " WHERE "+ COL + " ="+id;
SQLiteDatabase db = this.getReadableDatabase(); // ======> the emulator points here
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null)
cursor.moveToFirst();
StudentsinLib studentsinLib = new StudentsinLib();
studentsinLib.setId(cursor.getInt(0));
studentsinLib.setName(cursor.getString(1));
studentsinLib.setNumber(cursor.getString(2));
//log
Log.d("getStudent(" + id + ")", StudentsinLib.toString());
// 5. return student
return studentsinLib;
}
public void deleteStudentinLib(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(StudentsinLibDatabase, COL + " = ?",
new String[]{String.valueOf(id)});
db.close();
}
public int updateStudent(StudentsinLib studentsinLib) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(COL, studentsinLib.getId());
values.put(COL1,studentsinLib.getName());
values.put(COL2,studentsinLib.getNumber());
// 3. updating row
int i = db.update(STUDENTDATABASETABLE_NAME, //table
values, // column/value
COL + " = ?", // selections
new String[]{String.valueOf(studentsinLib.getId())}); //selection args
// 4. close
db.close();
return i;
}
public ArrayList<StudentsinLib> getAllStudentsinLib() {
ArrayList<StudentsinLib> studentsinLibs = new ArrayList<StudentsinLib>();
// 1. build the query
String query = "SELECT * FROM " + STUDENTDATABASETABLE_NAME;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build student and add it to list
while (cursor.moveToNext()) {
StudentsinLib studentsinLib = new StudentsinLib();
studentsinLib.setId(cursor.getInt(0));
studentsinLib.setName(cursor.getString(1));
studentsinLib.setNumber(cursor.getString(2));
studentsinLibs.add(studentsinLib);
}
Log.d("getAllStudents()", studentsinLibs.toString());
// return student
return studentsinLibs;
}
}
我之前尝试过的方法是
NullPointerException at this.getReadableDatabase();
NullPointerException on getReadableDatabase()
Sqlitedatabase query not working when selecting by _id
这就是我的全部故事,谢谢您……
答案 0 :(得分:1)
您要传递给Context
构造函数的StudentsinLibDatabase
参数为空。