Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:42:43+00:00 2026-05-25T18:42:43+00:00

I am parsing some URL links with the following.. Document jsDoc2 = null; try

  • 0

I am parsing some URL links with the following..

  Document jsDoc2 = null;
          try {
              jsDoc2 = Jsoup.connect(url).get();

                  Elements thumbs = jsDoc2.select("div.latest-media-images img.latestMediaThumb");
                  List<String> thumbLinks = new ArrayList<String>(); 
                  for(Element thumb : thumbs) {
                      thumbLinks.add(thumb.attr("src"));
                  }
                  for(String thumb : thumbLinks) {
                     url0 = thumbLinks.get(0);
                     url1 = thumbLinks.get(1); 
                     url2 = thumbLinks.get(2);

                     Log.e("URL0", url0);
                     Log.e("URL1", url1);
                     Log.e("URL2", url2);

After testing the code with multiple sources. ive ran across a problem.

09-19 20:59:56.710: ERROR/AndroidRuntime(7793): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
09-19 20:59:56.710: ERROR/AndroidRuntime(7793):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
09-19 20:59:56.710: ERROR/AndroidRuntime(7793):     at java.util.ArrayList.get(ArrayList.java:308)

Sometimes there arent three links availible and of cource when i ask to get the index of 2 and it doesnt exist it FC’s.

How can i code defensively or create a better way to implement this. The uncertainty of not knowing the exact amount of urls it will parse and load into the list

EDIT:

My COde now

 for(int i = 0; i< thumbLinks.size(); i++) {
                            Log.e("URL" + i, thumbLinks.get(i));




                     url0 = thumbLinks.get(i);
                     url1 = thumbLinks.get(i); 

                     //Fix index out of bounds exception
                     url2 = thumbLinks.get(i);



                  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:42:43+00:00Added an answer on May 25, 2026 at 6:42 pm

    Think this might work better for your loop structure

    for(int i = 0; i< thumbLinks.size(); i++) {
        Log.e("URL" + i, thumbLinks.get(i));
    }
    

    You could also just shorten this to something like:

    Elements thumbs = jsDoc2.select("div.latest-media-images img.latestMediaThumb");
    
    int index = 0;
    
    for(Element thumb : thumbs) {
        Log.e("URL" + index, (thumb.attr("src"));
        index++;
    }
    

    This second option reduces the operational time by reducing the looping structures and providing the same result. You will not get index out of bound errors with either of these because it will only add URLs if there are thumbs in your list.

    The reason your code is failing is because you’re explicitly trying to call indexes and essentially ignoring the loop structure all together. The reason to use a loop is so you don’t have explicitly get objects from the array and chance out of bounds exceptions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax() To
I am parsing some XML something like this: <root> <some_gunk/> <dupe_node> ... stuff I
I'm just in the process of parsing some text and can't remember what the
I've been parsing through some log files and I've found that some of the
What are some good tools for getting a quick start for parsing and analyzing
I'm doing some Objective-C programming that involves parsing an NSXmlDocument and populating an objects
I'm parsing big XML file with XPathExpression selection for some nodes existing at various
I am involved in a venture that will port some communications, parsing, data handling
SO I have the following set of code parsing delicious information. It prints data
SO I have the following set of code parsing delicious information. It prints data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.