0

I have a function which fetches a certain website's HTML code. Eventually that function will return the whole page as an array. I am trying to save the page in another array, but it will always throw an exception (ArrayIndexOutOfBoundsException) for some reason.

I am not sure if I am just missing something elementary or if it's more than just that. To give you some of my code:

  protected String[] doInBackground(String... login)
        {
            String[] page = new String[1024];
            try {
                page = executeHttpGet(); //THIS IS WHERE IT FAILS
            } catch (Exception e) {
                page[0] = "Error";
            }
            return page;
        }

public String[] executeHttpGet() throws Exception {
         URL u;
         InputStream is = null;
         DataInputStream dis;
         String s;
         int i = 0;
         String[] page = new String[1024];
         addSecurityException();
         Authenticator.setDefault(new MyAuthenticator(activity));
         try {
            u = new URL("https://myurl.com");
            is = u.openStream();        
            dis = new DataInputStream(new BufferedInputStream(is));
            while ((s = dis.readLine()) != null) {
                if (s.toString().length() > 10)
                    {
                    page[i] = s.toString();
                    i++;
                    }
            }
        } catch (IOException ioe) {

        }
         finally {
            try {
               is.close();
            } catch (IOException ioe) {

            }
        } 
         return page;
    }
    }

Does anyone have any ideas why I am not able to assign the returned array to the array in doInBackground()? Help would be greatly appreciated.

2
  • 1
    Which line causes the exception? What is the stack trace given by the exception? Commented Aug 30, 2011 at 10:41
  • Your program limits the reading of the website to 1024 lines. When the 1025th line is read, the statement is page[1024] = s.toString(), which is a disallowed index of your array. Commented Aug 30, 2011 at 10:56

1 Answer 1

7
  1. Your catch-blocks are empty, which is a terrible idea, because you'll get no information when something goes wrong.
  2. You don't need to initialize page on its declaraton, when you re-assign it two lines below (you'll have to change the catch-block to create a new array, 'though).
  3. Have you checked that your URL doesn't return more than 1024 lines?
  4. You don't need s.toString() when s is a String, because it will just return s.
  5. When analyzing an exception, you should look at the whole stacktrace to find out where exactly the problem occurs.
  6. When asking about an exception you should post the whole stacktrace!

When you have an unspecified number of elements List is a more flexible and easier-to-use alternative to arrays. Generally speaking, arrays are quite low-level and there's barely a reason to use them directly (they are very useful for building List implementations).

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, first of all, excuse my newbish behavior. Thanks for your help, it turns out the page I fetched was actually longer than the array, it was pretty stupid of me to not think of it on my own. Thanks for your other thoughts as well, I've made changes to my code and will keep it in mind for future projects.
@Tim: no need to appologize. Better to show it and learn than to hide it and keep those mistakes for a long time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.