Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:33:01+00:00 2026-06-17T09:33:01+00:00

I am fetching data from a database using this code : Cursor cursor =

  • 0

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 :
enter image description here

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:33:01+00:00Added an answer on June 17, 2026 at 9:33 am

    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)

    Cursor cursor = getACursor();
    int i = 0;
    while (cursor.moveToNext()) {
       /// do something with this row
       allNews[i] = new News(cursor.getInt(0), cursor.getString(2),cursor.getString(1));
       i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code for fetching data from database , I am getting
I'm new to NHibernate, so this is a basic question.. When fetching data from
i am using ADO.Net oledb for inserting and fetching data from Excel database. I
hi everyone i am fetching some data from a database using php, encoding it
I am fetching some data from a database like this $sql =SELECT * FROM
I'm currently fetching data from a MySQL database using JDBC and executeQuery . One
i am fetching this data from database : $sql = SELECT * FROM articles
I am fetching the data from a database and I am appending it to
I have an App using UITableViews and fetching data from a server. I am
This is my php script for fetching data from a db. <?php mysql_connect(host,user,pass); mysql_select_db(db);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.