I got an error "android.database.CursorIndexOutOfBoundsException: Index 20 requested, with a size of 20" and I can't understand what exactly caused it and how to fix it? Probably something wrong with c.moveToFirst() and c.moveToNext().

```
public class MainActivity extends AppCompatActivity {

    Map<Integer, String> articleURLs = new HashMap<Integer, String>();
    Map<Integer, String> articleTitles =new HashMap<Integer, String>();
    ArrayList<Integer> articleIds =new ArrayList<Integer>();

    SQLiteDatabase articlesDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        articlesDB=this.openOrCreateDatabase("Articles", MODE_PRIVATE, null);

        articlesDB.execSQL("CREATE TABLE IF NOT EXISTS articles (id INTEGER PRIMARY KEY, articleId INTEGER, url VARCHAR, title VARCHAR, content VARCHAR)");

        DownloadTask task = new DownloadTask();
        try {
            String result = task.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty").get();
            JSONArray jsonArray =new JSONArray(result);
            articlesDB.execSQL("DELETE FROM articles");

            for (int i=5;  i<25;i++){
                String articleId = jsonArray.getString(i);

                DownloadTask getArticle = new DownloadTask();
                String articleInfo = getArticle.execute("https://hacker-news.firebaseio.com/v0/item/"+ articleId+".json?print=pretty").get();

                JSONObject jsonObject= new JSONObject(articleInfo);

                Log.i("jsonObject", jsonObject.toString());
                String articleTitle = jsonObject.getString("title");
                String articleURL = jsonObject.getString("url");

                articleIds.add(Integer.valueOf(articleId));
                articleTitles.put(Integer.valueOf(articleId), articleTitle);
                articleURLs.put(Integer.valueOf(articleId), articleURL);

                String sql = "INSERT INTO articles (articleId, url, title) VALUES (?, ?, ?)";
                SQLiteStatement statement = articlesDB.compileStatement(sql);

                statement.bindString(1, articleId);
                statement.bindString(2, articleURL);
                statement.bindString(3, articleTitle);

                statement.execute();

            }
            Cursor c = articlesDB.rawQuery("SELECT * FROM articles",null);
            int articleIdIndex = c.getColumnIndex("articleId");
            int urlIndex = c.getColumnIndex("url");
            int titleIndex = c.getColumnIndex("title");

                c.moveToFirst();
            while (c!= null){
                Log.i("articleIdIndex", Integer.toString(c.getInt(articleIdIndex)));
                Log.i("articleUrl",c.getString(urlIndex) );
                Log.i("titleTitle",c.getString(titleIndex));
                c.moveToNext();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class DownloadTask extends AsyncTask< String, Void, String> {

        protected String doInBackground(String... urls) {

            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;
            try {
                url = new URL(urls[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);
                int data = reader.read();
                while (data != -1) {
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }
            }
                catch(Exception e){
                    e.printStackTrace();
                }
            return result;
            }
        }     
}

```
['https://Stackoverflow.com/questions/35957207', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5427240/']
Change the below lines to

```
c.moveToFirst();
while (c!= null){
    Log.i("articleIdIndex", Integer.toString(c.getInt(articleIdIndex)));
    Log.i("articleUrl",c.getString(urlIndex) );
    Log.i("titleTitle",c.getString(titleIndex));
    c.moveToNext();
}

```

to 

```
while (c.moveToNext()) {
    Log.i("articleIdIndex", Integer.toString(c.getInt(articleIdIndex)));
    Log.i("articleUrl",c.getString(urlIndex) );
    Log.i("titleTitle",c.getString(titleIndex));
}

```

> 
> The condition `(c!= null)` will always be `true`, so while loop will be executed every time even when there are no more records in database. So change the condition in while loop to `c.moveToNext()` so that it'll get record only when there are more records in database.
> 
> 
> 

And it'll work fine.
Change the below code

```
Cursor c = articlesDB.rawQuery("SELECT * FROM articles",null);
int articleIdIndex = c.getColumnIndex("articleId");
int urlIndex = c.getColumnIndex("url");
int titleIndex = c.getColumnIndex("title");

c.moveToFirst();
while (c!= null){
Log.i("articleIdIndex", Integer.toString(c.getInt(articleIdIndex)));
Log.i("articleUrl",c.getString(urlIndex) );
Log.i("titleTitle",c.getString(titleIndex));
c.moveToNext();
}

```

to the following

```
Cursor c = articlesDB.rawQuery("SELECT * FROM articles",null);
int articleIdIndex = c.getColumnIndex("articleId");
int urlIndex = c.getColumnIndex("url");
int titleIndex = c.getColumnIndex("title");

if(c.moveToFirst())
do{
     Log.i("articleIdIndex", Integer.toString(c.getInt(articleIdIndex)));
     Log.i("articleUrl",c.getString(urlIndex) );
     Log.i("titleTitle",c.getString(titleIndex));
}while(c.moveToNext());

```