add boundary check to avoid NullPointerException at long run case