Invalid index 3, size is 3 Android
<p>In my application I want to navigate left and right and change textView value while clicking on next and back button. But when the index is greater than the number of items I get a force close error. Here is my code.</p>

<pre><code>    String[] seperated = imag;

        count_image = seperated.length-1;

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new ImagePagerAdapter(seperated));

        pager.setCurrentItem(pagerPosition);

        options = new DisplayImageOptions.Builder()

        .showImageForEmptyUri(R.drawable.ic_launcher)

        .cacheOnDisc()

        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)

        .build();

        pager.setOffscreenPageLimit(3);



        @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub


        switch (v.getId()){

        case R.id.boutton_slide_droite:


            focus_image = pager.getCurrentItem();

            //get current image index 

            focus_image++;


            //move to next record

            position++;

            System.out.println("focus_image_up:"+focus_image);


            if(focus_image&lt;count_image){

                if(focus_image&gt;=count_image){

                    focus_image=0;
                    pagerPosition=0;

                }else {


                    pager.setCurrentItem(focus_image);

                    disable_progress=true; 

                    System.out.println("focus_image up if ++: 

"+focus_image);



                        text_num.setText("No. "+PostList.get(focus_image).getNum());
                        text_name.setText(PostList.get(focus_image).getNom());

                        text_team.setText(PostList.get(focus_image).getTeam());
                        text_date.setText(PostList.get(focus_image).getNaissance());

                    if 
(PostList.get(position).getNBVictoire().equals("1"))

                        text_victoires.setText("Victoire: "+PostList.get(focus_image).getNBVictoire());

                    else 
                        text_victoires.setText("Victoires: "+ PostList.get(focus_image).getNBVictoire());


                    if (PostList.get(focus_image).getNBCourse().equals("1"))

                        text_nomb.setText("Nombre de course: "+PostList.get(focus_image).getNBCourse());

                    else 

                        text_nomb.setText("Nombre de courses: "+PostList.get(focus_image).getNBCourse());

                }

            }else {

                pagerPosition=count_image;

                focus_image=count_image;
        }


            break;




         case R.id.boutton_slide_gauche:

             focus_image = pager.getCurrentItem();

             focus_image--;

            //move to previous record

            position--;


            if (focus_image&gt;0){



                if (focus_image&lt;=count_image) {

                    System.out.println("focus_image_down: if "+focus_image);


                    pager.setCurrentItem(focus_image);

                    disable_progress=true;

                    text_num.setText("No. "+PostList.get(focus_image).getNum());
                    text_name.setText(PostList.get(focus_image).getNom());

                    text_team.setText(PostList.get(focus_image).getTeam());

                    text_date.setText(PostList.get(focus_image).getNaissance());


                    if (PostList.get(focus_image).getNBVictoire().equals("1"))

                            text_victoires.setText("Victoire: "+PostList.get(focus_image).getNBVictoire());

                    else 

                        text_victoires.setText("Victoires: "+ PostList.get(focus_image).getNBVictoire());


                    if (PostList.get(position).getNBCourse().equals("1"))

                        text_nomb.setText("Nombre de course: "+PostList.get(focus_image).getNBCourse());
                    else 
                        text_nomb.setText("Nombre de courses: "+PostList.get(focus_image).getNBCourse());


                }else {


                    pager.setCurrentItem(focus_image);

                    disable_progress=true;

                    text_num.setText("No. "+PostList.get(focus_image).getNum());


text_name.setText(PostList.get(focus_image).getNom());


text_team.setText(PostList.get(focus_image).getTeam());


text_date.setText(PostList.get(focus_image).getNaissance());


                    if 

(PostList.get(focus_image).getNBVictoire().equals("1"))

                        text_victoires.setText("Victoire: "+PostList.get(focus_image).getNBVictoire());

                    else 


                        text_victoires.setText("Victoires: "+ PostList.get(focus_image).getNBVictoire());


                    if 

(PostList.get(position).getNBCourse().equals("1"))


                        text_nomb.setText("Nombre de course: "+PostList.get(focus_image).getNBCourse());


                    else 


                        text_nomb.setText("Nombre de courses: "+PostList.get(focus_image).getNBCourse());



                }


            }else {

                focus_image=0;
                pagerPosition=0;

            }


            break;
}   
</code></pre>

<p>and the error is </p>

<p>java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3</p>
