1

I am getting StringIndexOutOfBounds exception in this part of the code.
The field that captures myList is optional in the UI.This error occurs only when myList/myURL string is empty. How do I handle it?

Can someone correct me what am I doing wrong here?

    if (myList != null) {

            for (int r = 0; r < myList.size(); r++) {
                myURL = myURL + myList.toString() + ",";
            }

            myURL = myURL.substring(0, myrssURL.length() - 1);
            myURL = myURL.replace("[", "").replace("]", ""); 
        }
   else
    {
        rssList.clear();
        rssURL=null;
        System.out.println("inside else >>>>");
    }

2 Answers 2

8

If your length is 0, myURL = myURL.substring(0, myrssURL.length() - 1); will evaluate to myURL = myURL.substring(0,-1); Which is where you are getting your out of bounds error.

To fix, you should check if the arrayList.isEmpty() or if the length is 0.

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

Comments

4

Check for empties?

if (myList != null && !myList.isEmpty()) {

// same code as before
}

Comments

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.