-2

I got the following exception in my program :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at id.co.ptap.text.ParseText.main(ParseText.java:370)

This is the relevant code:

        x = first+";"+allHeader.get(count) + strBody + "\n"; //line 370
        bw.write(x);
        System.out.println(x);

            if (ketemu) {                   
                count++;
                ketemu = false;
            }
Eran
  • 387,369
  • 54
  • 702
  • 768
  • `allHeader` size is less than from `count`. – bNd Nov 04 '14 at 04:30
  • You already posted the same question: http://stackoverflow.com/questions/26707733/java-lang-indexoutofboundsexception-index-1-size-1 – Robby Cornelissen Nov 04 '14 at 04:31
  • I have chosen to close the other as a duplicate of this since this one has answers and is at least somewhat of an improvement, and I have chosen to close *this* one as a duplicate of an easily findable existing question on the general topic. – Jason C Nov 04 '14 at 04:41

3 Answers3

2

Well, based on the error allHeader is an ArrayList with a single item, and count == 1. The max index accepted by allHeader.get(count) is allHeader.size() - 1.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

allHeader.get(count)

will give you this Exception

check value of Count.

because you are trying to access value that doesn't have matching index number for ArrayList you have to access value from ArrayList which is less than size of ArrayList. (i.e value of count should be less than size of ArrayList)

count < ArrayList.size()

Nirav Prajapati
  • 2,987
  • 27
  • 32
1

ArrayList indexes start at 0, and go up to size() - 1. So if you have 1 item, it is at index 0, not 1. Change the following line to:

**x = first+";"+allHeader.get(count-1) + strBody + "\n";** //line 370

That would get the last item in the ArrayList, assuming count is equal to allHeader.size()

Shadow
  • 3,926
  • 5
  • 20
  • 41
  • i already try but this exception : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(Unknown Source) at java.util.ArrayList.get(Unknown Source) at id.co.ptap.text.ParseText.main(ParseText.java:370) – Permata Sari Nov 04 '14 at 07:21
  • Is `count` equal to `allHeader.size()`? The only way you'd get an IndexOutOfBounds at `-1` using the above code is if count was `0`, which means that there are no items in the ArrayList. – Shadow Nov 04 '14 at 11:09