Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Normaly, this code working very well. But, if "cursor" is empty, there is an error in main.class. I tried a lot of thing. But, I didn't success. Please help to solve way.

---- database.class ----

Java
public List<Integer> count_a() {
    List<Integer> list = new ArrayList<Integer>();

    String selectQuery = "select book, count(date) from myTAB WHERE (date>'" + 0 + "') group by book"; 
// if result is empty, there is an error in main.class.
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            list.add(Integer.parseInt(cursor.getString(1)));
        }
        while (cursor.moveToNext());
    }

    cursor.close();
    db.close();
    return list;
}

---- main.class ----

Java
private void load_data() {
    Database db = new Database(getApplicationContext());

    List<Integer> co_a = db.count_a(); // error is here

    Integer[] co_b = new Integer[co_a.size()];
    co_b = co_a.toArray(co_b);

    List_Row adapter = new List_Row(this, co_b);
    ListView lv = (ListView) findViewById(R.id.lvStat);
    lv.setAdapter(adapter);
}


What I have tried:

Failure 1

Java
if (cursor.moveToFirst()) {
        do {
            if (cursor.getCount() == 0 || cursor == null) {
                list.add(0);
            } else {
                list.add(Integer.parseInt(cursor.getString(1)));
            }
        }
        while (cursor.moveToNext());
    }

Failure 2

Java
if (cursor.moveToFirst()) {
        do {
            if(cursor.getCount() == 0){
                list.add(0);
            }else {
                list.add(Integer.parseInt(cursor.getString(1)));
            }
        }
        while (cursor.moveToNext());
    }

Failure 3

Java
if (cursor != null &amp;&amp; cursor.moveToFirst()) {
        do {
            if (cursor.getCount() &gt; 0) {
                list.add(Integer.parseInt(cursor.getString(1)));
            } else {
                list.add(0);
            }
        }
        while (cursor.moveToNext());
    }

Failure 4

Java
if (cursor.moveToFirst()) {
        do {
            if (cursor != null &amp;&amp; cursor.getCount() &gt; 0) {
                list.add(Integer.parseInt(cursor.getString(1)));
            } else {
                list.add(0);
            }
        }
        while (cursor.moveToNext());
    }

Failure 5

Java
if (cursor.moveToFirst()) {
        do {
            list.add(Integer.parseInt(cursor.getString(1)));
        }
        while (cursor.moveToNext());
    }
    if (cursor.isBeforeFirst()){
        do {
            list.add(0);
        }
        while (cursor.moveToNext());
    }
Posted
Updated 9-Dec-16 8:59am
Comments
Peter Leow 6-Dec-16 10:21am    
What do you mean by 'Normaly, this code working very well'? What is the error message? How do you know it is due to empty cursor? The selectQuery does not seems correct.
Richard Deeming 6-Dec-16 12:26pm    
And if you want someone to help you fix an error, then you need to give us the details of the error!

1 solution

The general pattern to test for a "valid" cursor is :
C#
if(cursor != null && cursor.getCount() > 0)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900