列出数据库Nullpointer exeption

时间:2017-12-23 14:38:12

标签: java sqlite android-recyclerview nullpointerexception android-context

这是我的数据库助手类。我正在调用createWord methot创建数据库。这是工作。但是当我调用listEnWords方法在recyclerview上显示时,我正在使用nullpointerexeption。我认为我的背景是错误的。我应该如何改变我的背景?

  

java.lang.NullPointerException:尝试调用虚方法'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String,int,android.database.sqlite.SQLiteDatabase $ CursorFactory,android。 data.DatabaseErrorHandler)'对空对象引用

public class WordsDbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "words.db";
private Context context;

public WordsDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context=context;
}


@Override
public void onCreate(SQLiteDatabase db) {
    final String SQL_CREATE_BOOKS_TABLE =

            "CREATE TABLE " + WordEntry.table_WORDS + " (" +

                    WordEntry.word_iD + " INTEGER PRIMARY KEY AUTOINCREMENT, " +

                    WordEntry.word_ENG + " TEXT NOT NULL, " +

                    WordEntry.word_TR + " TEXT NOT NULL )";

    db.execSQL(SQL_CREATE_BOOKS_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + WordEntry.table_WORDS);
    onCreate(db);
}

public void createWord(Words book) {
    String table_WORDS = "words";
    String word_ENG = "engWord";
    String word_TR = "trWord";

    // get reference of the BookDB database
    SQLiteDatabase db = this.getWritableDatabase();

    // make values to be inserted
    ContentValues values = new ContentValues();
    values.put(word_ENG, book.getEngWord());
    values.put(word_TR, book.getTrWord());

    // insert book
    db.insertWithOnConflict(table_WORDS, null, values, SQLiteDatabase.CONFLICT_IGNORE);

    // close database transaction
    db.close();
}

public List<String> listEnWords() {
    List<String> dataEng = new ArrayList<>();

    SQLiteDatabase db = this.getWritableDatabase(); //Error is here
    String[] columns = {WordEntry.word_iD, WordEntry.word_ENG, WordEntry.word_TR};

    Cursor cursor = db.query(WordEntry.table_WORDS, columns, null,
            null,
            null,
            null,
            null);

    while (cursor.moveToNext()) {
        dataEng.add(cursor.getString(0) + "-" + cursor.getString(1));

    }
    return dataEng;
}

public List<String> listTrWords() {
    List<String> dataTr = new ArrayList<>();

    SQLiteDatabase db = this.getWritableDatabase();
    String[] columns = {WordEntry.word_iD, WordEntry.word_ENG, WordEntry.word_TR};

    Cursor cursor = db.query(WordEntry.table_WORDS, columns,
            null,
            null,
            null,
            null,
            null);

    while (cursor.moveToNext()) {
        dataTr.add(cursor.getString(2));

    }
    return dataTr;
}

我正在调用来自recyclerviev适配器的list方法。

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    WordListRecyclerItemViewHolder holder = (WordListRecyclerItemViewHolder) viewHolder;

    WordsDbHelper helper = new WordsDbHelper(null);

    List<String> enWordData = helper.listEnWords();
    List<String> trWordData = helper.listTrWords();

    String itemEnText = enWordData.get(position);
    String itemTrText = trWordData.get(position);

    holder.setNewsTitle(itemEnText + ". word");
    holder.setNewsDesc(itemTrText + ". kelime");
}

1 个答案:

答案 0 :(得分:0)

WordsDbHelper helper = new WordsDbHelper(null);

当您实例化此类super(context,DATABASE_NAME,null,DATABASE_VERSION)将被调用,其上下文为null。 SQLiteOpenHelper不接受空上下文。您应该提供一个。