Added out-of-range handling to getChar method