I am downloading some data into a String array. Let’s say ImageLinks.
How do I check if a item in array exist?
I am trying
if(ImageLinks[5] != null){}
but it gives me ArrayIndexOutOfBoundsException. (Because there are really no 5 link in the array)
To prevent the
ArrayIndexOutOfBoundsException, you can use the following:As the statements in the
ifare checked from left to right, you won’t reach the null check if the array doesn’t have the correct size.It’s quite easy to generalise for any scenario.