Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 >>>>");
    }
share|improve this question

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer

Check for empties?

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

// same code as before
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.