0

This is my database helper class. I am calling createWord methot to create databese. It is working. But when I calling listEnWords method to display on recyclerview I am taking nullpointerexeption. I think my context is wrong. How should I change my context?

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference

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;
}

I am calling list methods from adapter of recyclerviev like this.

@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");
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0
WordsDbHelper helper = new WordsDbHelper(null);

When you instantiate this class super(context, DATABASE_NAME, null, DATABASE_VERSION) will be called having context null . SQLiteOpenHelper do not accept a null context.You should provide one.

Uta Alexandru
  • 324
  • 4
  • 11
  • I tried to write context instead of null. Like WordsDbHelper helper = new WordsDbHelper(context); But still same exeption – Erman Elmalı Dec 23 '17 at 14:51
  • read here https://stackoverflow.com/questions/12849930/passing-context-to-sqliteopenhelper .I think this will help you. – Uta Alexandru Dec 23 '17 at 14:56