Update some safety measure to make sure that NullPointerException and IndexOutOfRange exception are gracefully handled.