I am fetching data from a database using this code :
Cursor cursor = db.query(News_Table, null, null, null, null, null, null);
if (cursor != null)
{
cursor.moveToFirst();
}
allNews = new News[cursor.getCount()];
int i = 0;
while (!(cursor.isLast()))
{
allNews[i] = new News(cursor.getInt(0), cursor.getString(2),
cursor.getString(1));
cursor.moveToNext();
i++;
}
This is how my table looks :

But everytime my last row is not being fetched and i get a null pointer exception. Here is the log cat screen :

Why is my code running only for n-1 rows correctly and giving a problem on the (n)th row, where n is the total number of rows in my news table.
cursor.isLast() will return true when the cursor is on the last row, and so your while loop will stop at that point having not read & processed the last row. typically the pattern for iterating a cursor would be (note this doesn’t call
moveToFirst()either)