当我通过其id从SQLite数据库调用项目时,“SQLiteDatabase db = this.getReadableDatabase()”会导致错误:null对象引用

问题描述 投票:0回答:1

我有一个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();

PART: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

这就是我的故事,提前谢谢你......

android nullpointerexception android-sqlite android-database
1个回答
1
投票

您传递给Context构造函数的StudentsinLibDatabase参数为null。

© www.soinside.com 2019 - 2024. All rights reserved.