为什么我得到java.lang.IllegalArgumentException:在这种情况下,索引1处的绑定值为null?

时间:2015-04-21 02:49:30

标签: java android sqlite android-activity android-sqlite

我知道java.lang.IllegalArgumentException: the bind value at index 1 is null的含义以及导致错误的原因。我的情况我仍然无法找到出现这种错误的原因。

那么,我打算实现什么目标?

我的SQLite数据库表中有4列。我想找到每列的总和,然后再次找到这四个结果的总和。 sum的行值根据用户从autoCompleteTextView中选择的产品名称进行过滤。

我在哪里收到错误?

我的stackTrace说我在下面的代码中以 Cursor c = db.query ... 开头的行中收到错误。以下代码是我的android项目的db文件的一部分。

    public int addPurchaseQuantity(String itemName) {
    SQLiteDatabase db = helper.getReadableDatabase();
    int result = 0;
    String selection = VivzHelper.COLUMN_ADD_PURCHASE_ITEM_NAME + " =? ";
    String[] selectionArgs = {itemName};
    Cursor c = db.query(VivzHelper.ADD_PURCHASE_TABLE,
            new String[]{"sum(" + VivzHelper.COLUMN_ADD_PURCHASE_ITEM_QUANTITY + ")"},
            selection,
            selectionArgs,
            null,
            null,
            null);
    if (c.moveToFirst()) {
        result = c.getInt(0);
    }
    c.close();
    return result;
}

itemName是根据用户选择从autoCompleteTextView获得的。

        String[] autoCompleteName = vivzHelper.getInventoryNameFilterBySupplierName(vivzHelper.getSupplierID(param1));
    ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
    addPurchaseItemName.setThreshold(1);// starts working from first char
    addPurchaseItemName.setAdapter(NameAdapter);

    addPurchaseItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            itemName = String.valueOf(arg0.getItemAtPosition(arg2));
        }
    });

我的stackTrace也指向以下java活动文件的代码。

int purchaseQty = vivzHelper.addPurchaseQuantity(itemName);

这是我的stackTrace

04-21 00:09:58.274  21975-21975/com.example.bharathduraiswamy.comboedittext E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bharathduraiswamy.comboedittext/com.example.bharathduraiswamy.comboedittext.AddPurchase}: java.lang.IllegalArgumentException: the bind value at index 1 is null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2313)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
        at android.app.ActivityThread.access$600(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5336)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
        at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
        at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
        at com.example.bharathduraiswamy.comboedittext.VivzDatabaseAdapter.addPurchaseQuantity(VivzDatabaseAdapter.java:2011)
        at com.example.bharathduraiswamy.comboedittext.AddPurchase.aggregateQty(AddPurchase.java:531)
        at com.example.bharathduraiswamy.comboedittext.AddPurchase.onCreate(AddPurchase.java:133)
        at android.app.Activity.performCreate(Activity.java:5122)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
        at android.app.ActivityThread.access$600(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5336)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

有人可以告诉我其实在哪里错了吗?

更新01:添加了AddPurchase活动的onCreate()

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_purchase);

    // Set a toolbar to replace the action bar
    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_addPurchase);
    setSupportActionBar(myToolbar);
    myToolbar.setNavigationIcon(R.drawable.logo);

    //Disable the Toolbar Title
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    // Adding UP icon for Setting Activity
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    SharedPreferences myPreferences = getSharedPreferences("sharedSupplierData", Context.MODE_PRIVATE);
    param1 = myPreferences.getString("sharedSupplierName", DEFAULT);
    if (param1.equals(DEFAULT)) {
        Message.message(this, "No data was found");
    }

    addPurchaseItemName = (AutoCompleteTextView) findViewById(R.id.addPurchaseProductName);
    addPurchaseItemQty = (EditText) findViewById(R.id.addPurchaseProductQuantity);
    addPurchaseCostPrice = (EditText) findViewById(R.id.addPurchaseCostPrice);
    addPurchaseSalePrice = (EditText) findViewById(R.id.addPurchaseSalePrice);
    myDate = (TextView) findViewById(R.id.addPurchaseDatum);
    purchaseSum = (TextView) findViewById(R.id.addPurchaseSum);
    primarySpinner = (Spinner) findViewById(R.id.toolbar_spinner);
    vivzHelper = new VivzDatabaseAdapter(this);

    showDialogOnButtonClick();
    populateListView();

    String[] autoCompleteName = vivzHelper.getInventoryNameFilterBySupplierName(vivzHelper.getSupplierID(param1));
    ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
    addPurchaseItemName.setThreshold(1);// starts working from first char
    addPurchaseItemName.setAdapter(NameAdapter);

    addPurchaseItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            itemName = String.valueOf(arg0.getItemAtPosition(arg2));
        }
    });

    Message.message(this, ("Item Name  = " + itemName));

    aggregateQty();

    //Initializing an Adapter
    ArrayAdapter<String> toolbar_adapter = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_dropdown_item, primarySpinner_array);
    //Providing the Resource xml for Drop down Layout
    toolbar_adapter.setDropDownViewResource(R.layout.custom_simple_spinner_dropdown_item);
    primarySpinner.setAdapter(toolbar_adapter);

    //Setting a default Spinner value before onItemClick
    primarySpinner.setSelection(0);
    //Initializing an OnItemClick Listener for Spinner Item
    primarySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            int position_toolbar = primarySpinner.getSelectedItemPosition();

            //Changing Spinner TextSize and Text Color
            ((TextView) arg0.getChildAt(0)).setTextColor(Color.WHITE);
            ((TextView) arg0.getChildAt(0)).setTextSize(20);
            //Changing Spinner Pointer Color - partially effective
            primarySpinner.getBackground().setColorFilter(getResources().getColor(R.color.White), PorterDuff.Mode.SRC_ATOP);

            //Using Switch to move to other Activities
            Intent intent_spinner;
            switch (arg2) {
                case 1:
                    intent_spinner = new Intent(AddPurchase.this, SupplierDues.class);
                    AddPurchase.this.startActivity(intent_spinner);
                    break;
                case 2:
                    intent_spinner = new Intent(AddPurchase.this, SupplierReturns.class);
                    AddPurchase.this.startActivity(intent_spinner);
                    break;
                case 3:
                    intent_spinner = new Intent(AddPurchase.this, SupplierBalanceSheet.class);
                    AddPurchase.this.startActivity(intent_spinner);
                    break;
                case 4:
                    intent_spinner = new Intent(AddPurchase.this, AddSupplier.class);
                    AddPurchase.this.startActivity(intent_spinner);
                    break;
                default:
                    break;
            }
            //Changing Spinner Pointer Color - partially effective
            primarySpinner.getBackground().setColorFilter(getResources().getColor(R.color.White), PorterDuff.Mode.SRC_ATOP);
            //Setting a default Spinner value after onItemClick
            primarySpinner.setSelection(0);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });
}

更新02:调用addPurchaseQuantity()时的aggregateQty()

    public void aggregateQty() {

    int purchaseQty = vivzHelper.addPurchaseQuantity(itemName);
    int saleQty = vivzHelper.addSaleQuantity(itemName);
    int supplierRtnQty = vivzHelper.returnedToSupplierQuantity(itemName, rtnPrice);
    int supplierRtnScrapQty = vivzHelper.returnedToSupplierForScrapQuantity(itemName, rtnPrice);
    int customerRtnQty = vivzHelper.returnedByCustomerQuantity(itemName, rtnPrice);

    if (itemName.length() != 0) {
        availableQuantity = String.valueOf(purchaseQty - saleQty - supplierRtnQty - supplierRtnScrapQty + customerRtnQty);
        addPurchaseItemQty.setHint(availableQuantity);

        Message.message(this, ("Item Name  = " + itemName + "\n" +
                "Purchase Quantity = " + purchaseQty + "\n" +
                "Sale Quantity = " + saleQty + "\n" +
                "Supplier Return Quantity = " + supplierRtnQty + "\n" +
                "Customer Return Quantity = " + customerRtnQty));

    } else {
        Message.message(this, "Please Select an Item");
    }
}

2 个答案:

答案 0 :(得分:1)

itemNamenull调用中addPurchaseQuantity(),因此当SQLite尝试使用它来替换选择参数(&#39;?&#39;)时,您会收到此异常

尝试将itemName设置为已知值并检查结果。

修改

您应该独立测试每个方法,以便轻松发现错误或错误。学习Testing Fundamentals

中所述的单元测试

然后,您会发现问题已经减少,以便从AutoCompletTextView中获取文本值,您可以在其中找到大量示例(例如How to get string text from AutoCompleteTextView?)。

答案 1 :(得分:-1)

我会尝试删除总和:new String [] {“sum(”+ VivzHelper.COLUMN_ADD_PURCHASE_ITEM_QUANTITY +“)”}, 看看绑定是否有效。