views:

47

answers:

2

im testing to add 300 contacts in my app and it was doing fine with desire but in galaxy it crashes and does not show my 300 contacts and they erased the contacts after the crash .

anyone saw this?

+1  A: 

simple answer:

StringIndexOutOfBoundsException

Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.

maybe you are accessing your string with charAt() method? either way, accessing a position in the string that is not there will return an array like yours.

    String myString;
    myString = "Hello";
    System.out.println(charAt(10)); will cause a stringsIndexOutOfBoundsException

Detailed answer:

explains what happens before any sort of indexoutofboundsexception is thrown. I've used arrays as a base example.

It means that you are iterating beyond the 300 contacts. Ut has reached the end of the array and tried to access the next value which is empty. this returns an indexoutofboundsexception.

When looping through your array you need to understand that element one, the first element in the array, is defined as 0. Because java has an index that starts at 0. So accessing th first element you would do like so myArray[0];

so If you want to access the last element of your array you do something like myArray[myArray.size - 1]

So if you have 10 elements you will have an array that looks like this:

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Note there is no 10 as it starts from 0.

so when you try to access element 10 you do so like this. myArray[myArray.size-1]

myArray.size will return the size of the array which is equal to 10 then we minus 1 to get a value of 9 then we can access the tenth element by putting value 9.

This is a neat of of catching any errors without your application during run time. instead it produces a message for the user to notify them of the error.

PK

Pavan
As it's a `StringIndexOutOfBoundsException`, an array doesn't have anything to do with it.
Colin Hebert
i dispute that. A string is an array. it is an array of characters.Hes getting a stringindexoutofboundsexception which is thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string. so he is trying to access a part of the string which is not there. Im just explaining the logic to him by giving example of an array. he'll know what to do.
Pavan
@Pavan, My point is, as you don't access String internal like you access an array, your answer isn't really relevant. Plus, your post doesn't answers the question at all. You're explaining "how to catch exception" to someone who have a particular exception and doesn't know **why** it happens in his context.
Colin Hebert
its just solution number 2. its an option. not necessary. yes there is some information which may not be helpful to him, but it might be for other people. remember this site is for people to learn from each other. just incase someone else has an indexoutboundsexception they can have a quick read about what i wrote. In my answer ive also answered his question. hes wandering why he is getting the stringsoutofboundsexception which i explained at the top. if he wants to carry on reading about it so be.
Pavan
@Pavan, Contrary to what you seem to understand, this post isn't at all about handling exceptions, but about the real cause of this particular one.
Colin Hebert
@colin no problem thats fine. I know it has nothing to do with handling exceptions, but its something someone could consider if they ever should get an error like this and want there application not to crash.In this instance, i have addressed his situation as a simple answer on the top of my post. thats the main answer. His question is vague as he has not explained how he is accessing the string but regardless that should help him understand what is happening. Now if he would like to carry on reading the post just to find out a little bit more and ways to go about it in future then he can.
Pavan
@colin: I have simplified the answer as like you've said some of that info wasn't really necessary. The post is a lot clearer now. Thank you
Pavan
A: 

Well i guess it is the Samsung Galaxy S, still adding it in the view ... that is why it will take some time to render it on the device!

mikedroid