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.

0

3 Answers 3

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);
            } 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.