Fixed exception by adding length check

Fixed exception by adding length check