String Out of Bounds Exception

I have been facing the String Out of Bounds Exception quite a lot when writing codes for Strings. I dont understand why this occurs. Can u pls give me an explanation as to how I can avoid it?

@code_breaker-1001 Your code for the strings-string compression problem is running good and passing all the test cases.
But if you just want to know why this error occurs I will help you with this.
The StringIndexOutOfBoundsException is one of the unchecked exceptions in Java. A string is kind of an ensemble of characters. String object has a range of [0, length of the string] . When someone tries to access the characters with limits exceeding the range of actual string value, this exception occurs.
So while running loops and all check whether you are trying to access the unreachable index of the string or not.