Wednesday, 14 August 2013

StringIndexOutOfBoundException for unknown reason

StringIndexOutOfBoundException for unknown reason

I have no clue, why I get an StringIndexOutOfBoundExeption at this point:
if (!dataList.get(i).toString().contains("null")){
shortList.add(dataList.get(i).toString()); }
else {shortList.add("");}
if (!dataList.get(i+1).toString().contains("null")){
shortList.add(dataList.get(i + 1).toString());}
else {shortList.add("");}
if (!dataList.get(i+2).toString().contains("null")){
shortList.add(dataList.get(i + 2).toString());}
else {shortList.add("");}
if (!dataList.get(i+3).toString().contains("null")){
shortList.add(dataList.get(i + 3).toString()); }
else {shortList.add("");}
if (!dataList.get(i+4).toString().contains("null")){
shortList.add(dataList.get(i + 4).toString()); }
else {shortList.add("");}
This is inside a loop, which adds the data from this ArrayList:
[null, 00010, 2013, 2.0, null, and so on. As you can see, the first and
fifth element is null.
When I check the first element, for if it contains "null", it throws the
Exception. When I check the fifth element, for if it contains "null", it
doesn't, and displays the " " in the UI.
What is the reason for this?
And if I comment the if-statement for the first element out, my UI shows
me "null". (This is code from my android application, the TextView, which
displays the first element of the ArrayList displays "null")

No comments:

Post a Comment