1

Am a beginner at android java.

Error on SQLiteDatabase db = this.getWritableDatabase(); line in

public boolean deleteItem(Todomodel todomodel){

    SQLiteDatabase db = this.getWritableDatabase(); //HERE
    ContentValues cv = new ContentValues();
    int numb = todomodel.getId();
    String whereClause = "id=?";
    String whereArgs[] = {String.valueOf(numb)};
    db.delete("CUSTOMER_TABLE", whereClause, whereArgs);
    db.close();

    return false;
}

ERROR :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.monday, PID: 9794
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
        at android.content.ContextWrapper.getDatabasePath(ContextWrapper.java:330)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:292)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
        at com.example.monday.database.deleteItem(database.java:72)

I already had called the getWritabledatabase in the insert function. But when I try to write again. It doesn't work.

3
  • check your database context if you are passing it, check your read/write storage permissions, check whether you are initialize or opening your database before performing query! Commented Jun 3, 2021 at 11:18
  • @M.BilalAsif I have checked various site and it all looked the same code. What you mean by passing the context. I have passed a model to the function and I get the correct output on the other side. and first time calling the database class I have passed the this.mainactivity.. Commented Jun 3, 2021 at 12:20
  • okay have you check your database path this should be checked carefully! Commented Jun 3, 2021 at 12:21

1 Answer 1

1

You need to pass a valid Context to your SQLiteOpenHelper constructor.

Sign up to request clarification or add additional context in comments.

3 Comments

I have passed this.Mainactiviy to while creating a object for the database class like I did for the insert function which works perfect.
The Context can still be invalid. Please post code around how you are contructing the db helper, and maybe also how you are calling that code.
yes it works! but to call the update list which holds the set adapter is now the task that is in which I can't pass the context to from the adapter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.