for-loop to do-while
Change for-loop to do-while, if required add a null check.