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 != nulland then three lines later use it as if it couldn't be null withcursor.getCount()either the null check isn't required, or your code cannot handle null values.