'的NullPointerException'尝试从SQLite中删除时出错

时间:2014-07-27 11:30:23

标签: android sqlite android-sqlite

我有以下方法从我的数据库中删除数据:

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

我想要实现的目标的简短说明:用户想要从列表中删除旅程,这意味着必须删除此旅行的所有selected_groups,以及属于每个selected_group的所有selected_items。

当我运行此代码时,我收到以下错误消息:

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​​说:

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

当我试图注释掉db.delete语句并启用Toast消息时 - 它们都正常工作。但是试图删除...我尝试使用foreach循环而不是for循环,但是使用相同的错误消息。

为什么会发生这种情况?如何解决?

0 个答案:

没有答案