3

When using 12 in iteration (inside for) it output correctly, when assigning 13 to iterator (inside for) throws IndexOutOfBoundsException on line

imageUrl[i]=images.select("img").get(i).absUrl("data-original");`.

Note: class lazy in hasClass("lazy") has 12 elements:

Elements headlines = document.select("div.newscat a[href] h3");
Elements images=document.select("div.newscat").select("a[href]");
int lengthHeadline = headlines.size(); // outputs 13
String[] newsHeadline=new String[lengthHeadline];
String[] imageUrl=new String[lengthHeadline]

for(int i=0; i<12; i++){

    if (images.get(i).children().hasClass("lazy")){
        imageUrl[i] = images.select("img").get(i).absUrl("data-original");
    }else{
        imageUrl[i] = "http://something.com/8046.png";
    }
    System.out.println(imageUrl[i]);

}

EDIT:

HTML code

Here I have let say n number of .newscat classes and each of them contains picture and headlines, but in some cases they hold no image but headline only, so instead of blank-display I want to display any other image. So my image array should math that of headline.

enter image description here

The View that I want to be displayed (something like that).

enter image description here

Farid
  • 2,317
  • 1
  • 20
  • 34
  • 1
    I guess this is the part crashing : "images.select("img").get(i).absUrl("data-original");" So your "img" element probably doesn't have 13 elements? – Andros Apr 04 '16 at 18:10
  • I am not sure what is strange here. If array/list has 12 elements it means that first element is indexed with 0 and last is indexed with 11 (length or size reduced by 1 because indexing starts from 0). So if you limit your loop with 13 then last possible non-13 value will be 12 which is still greater than index of last element. Anyway your question is kind of strange because you claim that its `lazy` class which has 12 elements (whatever that means) but you are not selecting any of that elements in your code. – Pshemo Apr 05 '16 at 15:05
  • @Pshemo Hi, appreciate your comment, maybe you skipped the point that I am creating 13 element array on lines `int lengthHeadline = headlines.size(); String[] imageUrl=new String[lengthHeadline]`. Here headlines consists of 13elements – Farid Apr 05 '16 at 15:23
  • @Andros Yes, absolutely but why, I am creating 13 element array on lines `int lengthHeadline = headlines.size(); String[] imageUrl=new String[lengthHeadline]` where headlines size is 13. – Farid Apr 05 '16 at 15:27
  • 1
    You don't seem to realize that `[i]` is not only place which is limited by some bounds. There is also `get(i)`. Try printing result of `images.select("img").size()` most probably it will be 12. – Pshemo Apr 05 '16 at 15:38
  • @Pshemo in case question lacks sufficient information,the line that I get Exception is `imageUrl[i] = images.select("img").get(i).absUrl("data-original");` not `get(i)`. – Farid Apr 05 '16 at 15:42
  • 1
    I am aware of that. But take a closer look at that line. You are using `i` in *two* places: `[i]` and `get(i)`. Both are limited by some bounds and these bounds don't need to be equal. – Pshemo Apr 05 '16 at 15:42
  • I changed it to another letter, checked, same case. Why that should be problem anyway – Farid Apr 05 '16 at 15:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108300/discussion-between-farid-and-pshemo). – Farid Apr 05 '16 at 15:53
  • I can't help you much without [SSCCE](http://sscce.org) / [MCVE](http://stackoverflow.com/help/mcve). – Pshemo Apr 05 '16 at 15:54
  • Also post full stacktrace so we would be sure which part of your code exactly thrown this exception. – Pshemo Apr 05 '16 at 16:04

1 Answers1

2

when assigning 13 to iterator (inside for) throws IndexOutOfBoundsException on line

imageUrl[i] = images.select("img").get(i).absUrl("data-original");

There's no check of the Element list size returned by images.select("img").

Instead try this:

if (images.get(i).children().hasClass("lazy")) {
    Elements imgs = images.select("img");
    if (imgs.size() < i) {
       imageUrl[i] = imgs.get(i).absUrl("data-original");
    } else {
       // Handle the edge case here...
    }
} else {
    imageUrl[i] = "http://something.com/8046.png";
}
Stephan
  • 41,764
  • 65
  • 238
  • 329