-1

I want to perform 2 query when button is clicked in adapter class, first is select data from table A and insert to Table B, the other is delete row from table A.

Adapter

 mdb = new MyDatabaseHelper(v.getContext());
 database = mdb.getReadableDatabase();
 deleteTask = new DeleteTask();
 Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE ID = ? ", new String[]{id+""}, null);
 if (cursor != null && cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
              String allTask = cursor.getString(cursor.getColumnIndex("Title"));
             String name = cursor.getString(cursor.getColumnIndex("Name"));
             String allTime = cursor.getString(cursor.getColumnIndex("Time"));
             String allDate = cursor.getString(cursor.getColumnIndex("Date"));
             insertDataToCompleteTab(id, name, allTask, allTime, allDate); //insert to Completed table
             deleteTask.deleteData(id); // delete the row of data from Task Table
       }
 }

DeleteTask

  public void deleteData(int id)
    {
        database = mdb.getWritableDatabase();
        database.delete(MyDatabaseHelper.TABLE__TASK, MyDatabaseHelper.ID1 + "=" + id,null);
        database.close();
    }

Error

java.lang.NullPointerException
            at com.example.seng.healthyapp.DeleteTask.deleteData(DeleteTask.java:124)
            at com.example.seng.healthyapp.adapter.AllAdapter$1.onClick(AllAdapter.java:139)
            at android.view.View.performClick(View.java:4230)

The error pointing to the deleteTask method. But I have done initialized them, why I still getting such error ?

Tony
  • 2,515
  • 14
  • 38
  • 71
  • @0xDEADC0DE come on, don't because of getting this error and you said that it is duplicated. I been reading this article plenty of time but still cannot solve – Tony Nov 25 '16 at 07:21
  • I don't see how it is not a duplicate of a question explaining what an NPE is and how to fix it. You clearly have an NPE in your own code which is (most likely) easily solved when debugging it – 0xDEADC0DE Nov 25 '16 at 07:23
  • Only you know which line has number 124. – CL. Nov 25 '16 at 08:52
  • @CL. I have mentioned in my post , it pointing to the deleteTask method. – Tony Dec 03 '16 at 16:47

3 Answers3

0

It works for me, try it... You can alter this code to delete all..

db= myDb.getWritableDatabase();
db.delete(TABLE_NAME, " ID=? " , new String[] {id},null);
db.close();
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
0

After searching here and there, I decided to remove deleteTask = new DeleteTask(); and just use deleteData(id) instead of deleteTask.deleteData(id);. Call the method inside adapter but not calling others class object in adapter.

Tony
  • 2,515
  • 14
  • 38
  • 71
-1

This is because you are passing null in delete query, please update your code like this and check

public void deleteData(int id)
    {
        database = mdb.getWritableDatabase();
        database.delete(MyDatabaseHelper.TABLE__TASK, MyDatabaseHelper.ID1 + "=?" , new String[] {id});
        database.close();
    }
Ready Android
  • 3,529
  • 2
  • 26
  • 40