0

I have a method which retrieves rows from an sqlite database and returns them in a cursor.I am retreiving values from the query ok as shown below.

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

Problem is

when checking column values for null.For example for a row with values (KEY_SHARE_NAME=name,KEY_MAX_PRICE=12,KEY_MIN_PRICE=null),the program raises a null pointer exceptiuon at "else if (max_price.equalsIgnoreCase("null"))" section.what am i doing wrong?I cant seem to point the exact issue.

Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
mungaih pk
  • 1,809
  • 8
  • 31
  • 57

3 Answers3

4

"null" and null are not the same. "null" is a String and read more about null at What is null in Java?. So you can not do the min_price.equalsIgnoreCase("null") for null check. Read How to check a string against null in java?


Change

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

to

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);
            } 
Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
2

null and String "null" are not the same thing. Therefore for example this code:

min_price.equalsIgnoreCase("null")

should be changed to:

min_price == null
Celebes
  • 1,371
  • 1
  • 12
  • 21
1
min_price.equalsIgnoreCase("null")

will throw a NullPointerException if min_price is null.

Also, the above statement compares with "null", which is a string. You want to compare it with null reference.

Use min_price == null in that case

njzk2
  • 38,969
  • 7
  • 69
  • 107
rocketboy
  • 9,573
  • 2
  • 34
  • 36