added checking for string length before invoking method substring(startIndex), logs added as well