check if index is valid before attempting to use it