samedi 27 décembre 2014

Take non-null values from 2 columns in 7-column table

I have an SQLite table such as this:


Table


Each row represents an object, and columns B - G represent data the object holds. As you can see, only columns B and C are gauranteed to be non-null.


What I want to do is to get all non-null data from columns E and F, in a first-in-first-out method (if both E and F have non-null values, we'll just take E first). I want to take the non-null values and add them to an ArrayList<String> object and then return the array. So, for the above table, my ArrayList would look like [String1, String2, String3, String4].


Below is what I have so far, but my program crashes. I think the array that is being returned is null for some reason.



db = dbHelper.getReadableDatabase();

ArrayList<String> array= new ArrayList<String>();

Cursor cursor = db.rawQuery("SELECT " + COLUMN_E + ", " + COLUMN_F + " FROM "
+ MY_TABLE + " WHERE " + COLUMN_E + " IS NOT NULL OR "
+ COLUMN_F + " IS NOT NULL", null);

if (cursor.getCount() > 0)
{
cursor.moveToFirst();

do
{
array.add(cursor.getString(cursor.getColumnIndex(COLUMN_E)));
array.add(cursor.getString(cursor.getColumnIndex(COLUMN_F)));
} while (cursor.moveToNext());

cursor.close();
}

return array;


Can anyone please help me out? Thank you!


Logcat when E is not null, F is null. The same occurs when F is non-null and E is null, and also when both are non-null. The exception occurs in another class when I try to convert the ArrayList to a String array:


java.lang.NullPointerException: Attempt to get length of null array


Aucun commentaire:

Enregistrer un commentaire