0

I have the following method to delete data from my db:

public void delete_trip(Trip trip_to_delete)
{
    open_writable();
    ArrayList<Selected_Group> arr_selected_groups = get_selected_groups_by_trip_id(trip_to_delete.getTrip_id());
    for(int i = 0; i<arr_selected_groups.size(); i++)
    {
        Selected_Group sg = arr_selected_groups.get(i);
        ArrayList<Selected_Item> arr_selected_items = get_selected_items_by_selected_group_id(sg.getSelected_group_id());
        for(int j = 0; j<arr_selected_items.size(); j++)
        {
            Selected_Item si = arr_selected_items.get(j);
            db.delete(Constants.Table_Names.TABLE_NAME_SELECTED_ITEMS, "_id="+si.getSelected_item_id(), null);
            //Toast.makeText(context, "Item id: "+si.getSelected_item_id(), Toast.LENGTH_SHORT).show();
        }

        db.delete(Constants.Table_Names.TABLE_NAME_SELECTED_GROUPS, "_id="+sg.getSelected_group_id(), null);
        //Toast.makeText(context, "Group id: "+sg.getSelected_group_id(), Toast.LENGTH_SHORT).show();
    }
    db.delete(Constants.Table_Names.TABLE_NAME_TRIPS, "_id="+trip_to_delete.getTrip_id(), null);
    //Toast.makeText(context, "Trip id: "+trip_to_delete.getTrip_id(), Toast.LENGTH_SHORT).show();
    close();
}

Short explanation of what I'm trying to achieve: the user wants to delete a trip from a list, that means that all selected_groups for this trip also must be deleted, and all selected_items that belong to each selected_group.

When I'm running this code I'm having the following error message:

07-27 14:10:17.246: E/AndroidRuntime(27929): FATAL EXCEPTION: main
07-27 14:10:17.246: E/AndroidRuntime(27929): java.lang.NullPointerException
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:96)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1741)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.mycompany.myapp.tasks.Task_DB_Manager.delete_trip(Task_DB_Manager.java:360)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.mycompany.myapp.fragments.Fragment_Trips.onContextItemSelected(Fragment_Trips.java:114)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.support.v4.app.Fragment.performContextItemSelected(Fragment.java:1601)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:2008)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:375)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3521)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.widget.AbsListView$1.run(AbsListView.java:3168)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.os.Handler.handleCallback(Handler.java:605)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.os.Looper.loop(Looper.java:137)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at java.lang.reflect.Method.invoke(Method.java:511)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 14:10:17.246: E/AndroidRuntime(27929):    at dalvik.system.NativeStart.main(Native Method)

Line 360 says:

db.delete(Constants.Table_Names.TABLE_NAME_SELECTED_ITEMS, "_id="+si.getSelected_item_id(), null);

When I tried to comment out the db.delete statements and enable the Toast messages - they all worked correctly. But trying to delete... I tried to use foreach loop instead of for loop, but with same error message.

Why is it happening and how can it be solved?

Igal
  • 5,833
  • 20
  • 74
  • 132
  • Are you sure `db` is not null? – Wundwin Born Jul 27 '14 at 11:37
  • @ANyarThar You had a point! I opened the db connection in the beginning of the method (with `open_writable();`) and closed it at the end, but now that I saw your question I tried to open it right before each `db.delete` execution and close it immediately after it (meaning 3 times in total) and then everything worked. Thanks! Can you post your comment as an answer so I could accept it, please? – Igal Jul 27 '14 at 11:46

0 Answers0