当我通过id获取一些值时,sqlite出了点问题

时间:2014-02-01 18:29:03

标签: android sqlite nullpointerexception

我的活动是当用户点击保存按钮时。它会将其保存到表格中。首先单击保存按钮即可,但是第二次单击保存按钮是错误。

这是我的活动代码

save_i.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            TableLayout TL = (TableLayout) findViewById(R.id.tableLayout);

            int countRow = TL.getChildCount();
            //loop
            for(int i=0;i<countRow;i++){
                TableRow TR = (TableRow) TL.getChildAt(i);
                //keep the values in any position
                EditText edIng = (EditText) TR.getChildAt(0);
                EditText edAmo = (EditText) TR.getChildAt(1);
                EditText edUni = (EditText) TR.getChildAt(2);
                //get editText to String
                String ingredient = edIng.getText().toString();
                String amount = edAmo.getText().toString();
                String unit = edUni.getText().toString();
                System.out.println(ingredient+"_"+amount+"_"+unit);

                //save all data to ingredient
                //1.) use menu to find some ingredient.
                ArrayList<IngredientEntry> ingredients = new ArrayList<IngredientEntry>();
                ingredients = db.getIngredientByID(menuID);
                if(ingredients == null){
                    System.out.println("new ingredient");
                    ingredientID = db.insertIngredient(ingredient, unit, null, menuID, amount);
                    System.out.println(ingredientID);
                }else{
                    System.out.println("have ingredient allready");
                }


            }

        }
    });

这是我的数据库解决方案

public ArrayList<IngredientEntry> getIngredientByID(int menuID){

    String menu_ID = Integer.toString(menuID);
    ArrayList<IngredientEntry> ingredients = new ArrayList<IngredientEntry>();
    ingredients = null;
    Cursor cursor = db.rawQuery("SELECT name FROM " + 
            BooksDBHelper.TABLE_INGREDIENT + " WHERE " +
            BooksDBHelper.KEY_MENU_ID + " = ?", 
            new String[]{menu_ID});
    cursor.moveToFirst();
    if(cursor.getCount() != 0){

        do{
            IngredientEntry ingredient = new IngredientEntry();
            ingredient.setName(cursor.getString(cursor.getColumnIndex(BooksDBHelper.KEY_NAME)));
            ingredients.add(ingredient);
        }while(cursor.moveToNext());

    }

    return ingredients;

}

首先点击它然后循环if.It是正确的。 第二次点击它应该去循环else.But它是错误。 这是我的logcat

02-02 01:10:36.286: E/AndroidRuntime(14242): FATAL EXCEPTION: main
02-02 01:10:36.286: E/AndroidRuntime(14242): java.lang.NullPointerException
02-02 01:10:36.286: E/AndroidRuntime(14242):    at mutitablesql.db.BooksDB.getIngredientByID(BooksDB.java:128)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at createbook_sub1.create_menu_write$3.onClick(create_menu_write.java:88)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at android.view.View.performClick(View.java:4232)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at android.view.View$PerformClick.run(View.java:17298)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at android.os.Handler.handleCallback(Handler.java:615)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at android.os.Looper.loop(Looper.java:137)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at android.app.ActivityThread.main(ActivityThread.java:4921)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at java.lang.reflect.Method.invoke(Method.java:511)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-02 01:10:36.286: E/AndroidRuntime(14242):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

删除该行:ingredients = null; 这是导致ingredients.add(ingredient);

时出错的原因