在android中比较两个字符串

时间:2013-09-11 13:36:46

标签: java android sqlite nullpointerexception cursor

我有一个方法从sqlite数据库中检索行并将它们返回到游标中。我正在从查询ok中检索值,如下所示。

    private void checkWatchList() {
    Log.i("watch list", "watch list started");
    Cursor cursor = null;
    try {
        cursor = dbhandler.getAlerts();
        while (cursor.moveToNext()) {
            Log.i("watch list", "checked");

            String share_name = cursor.getString(cursor
                    .getColumnIndex(KEY_SHARE_NAME));
            String max_price = cursor.getString(cursor
                    .getColumnIndex(KEY_MAX_PRICE));
            String min_price = cursor.getString(cursor
                    .getColumnIndex(KEY_MIN_PRICE));
            int action = cursor.getInt(cursor.getColumnIndex(KEY_ACTION));
            Log.i("name", share_name + "/" + min_price + "/" + max_price);

            if (min_price.equalsIgnoreCase("null")) {
                Log.i("min", "msg");
                double maxprice = Double.parseDouble(max_price);
                compareMax(share_name, maxprice, action);
            } else if (max_price.equalsIgnoreCase("null")) {
                Log.i("max", "msg");
                double minprice = Double.parseDouble(min_price);
                compareMin(share_name, minprice, action);
            } else {
                Log.i("minmax", "msg");
                double maxprice = Double.parseDouble(max_price);
                double minprice = Double.parseDouble(min_price);
                compareMinMax(share_name, minprice, maxprice, action);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cursor.close();
    }
}

问题是

在检查null的列值时。例如对于值为(KEY_SHARE_NAME=name,KEY_MAX_PRICE=12,KEY_MIN_PRICE=null)的行,程序会引发null pointer exceptiuon at "else if (max_price.equalsIgnoreCase("null"))"部分。我做错了什么?我似乎无法指出确切的问题。

3 个答案:

答案 0 :(得分:4)

"null"null不一样。 "null"是一个字符串,详细了解What is null in Java?处的null。因此,您无法执行min_price.equalsIgnoreCase("null")进行空检查。阅读How to check a string against null in java?


更改

if (min_price.equalsIgnoreCase("null")) {
                Log.i("min", "msg");
                double maxprice = Double.parseDouble(max_price);
                compareMax(share_name, maxprice, action);
            } else if (max_price.equalsIgnoreCase("null")) {
                Log.i("max", "msg");
                double minprice = Double.parseDouble(min_price);
                compareMin(share_name, minprice, action);
            } 

if (min_price == null) {
                Log.i("min", "msg");
                double maxprice = Double.parseDouble(max_price);
                compareMax(share_name, maxprice, action);
            } else if (max_price == null) {
                Log.i("max", "msg");
                double minprice = Double.parseDouble(min_price);
                compareMin(share_name, minprice, action);
            } 

答案 1 :(得分:2)

null和字符串"null"不是一回事。因此,例如此代码:

min_price.equalsIgnoreCase("null")

应更改为:

min_price == null

答案 2 :(得分:1)

min_price.equalsIgnoreCase("null")
如果min_price为null,

将抛出NullPointerException。

此外,上述语句与“null”比较,后者是一个字符串。您想将它与空引用进行比较。

在这种情况下使用min_price == null