我正在打电话给Yelp,以在我写的公寓查询器中获得某些地址的评论,但看起来某些Yelp数据不完整,因此以下代码导致Ye Olde ORNSTAIOAN错误:

    public IEnumerable<Review> GetReviews(Bounds searchBounds) {
        var yelp = new Y.Yelp(ConfigOptions);
        var searchOptions = GetSearchOptions(searchBounds);
        var searchTask = yelp.Search(searchOptions);
        var tasks = new Task<Y.Data.SearchResults>[GetTaskArraySize(searchTask)];

        tasks[0] = searchTask;

        for (var i = 1; i < tasks.Length; i++) {
            searchOptions.GeneralOptions.offset = i * YelpResultsLimit;
            tasks[i] = yelp.Search(searchOptions);
        }

        Task.WaitAll(tasks);

        return tasks.SelectMany(t => t.Result.businesses != null
            ? t.Result.businesses
            : null)
                    .Select(MapYelpBusinessToReview);
    }


即使我像上面一样尝试空捕获,错误也会在“企业”的返回行中出现。错误输出为:

<Error>
    <script id="tinyhippos-injected"/>
    <Message>
        An error has occurred.
    </Message>
    <ExceptionMessage>
        Object reference not set to an instance of an object.
    </ExceptionMessage>
    <ExceptionType>
        System.NullReferenceException
    </ExceptionType>
    <StackTrace>
        at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ApartmentFinder.Infrastructure.Services.SearchEngine.<>c__DisplayClassc.<FindProperties>b__1() in c:\Development\myProject\ApartmentFinder.Infrastructure\Services\SearchEngine.cs:line 25 at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute()
    </StackTrace>
</Error>


如何捕获丢失的业务信息,然后继续/不进行处理?

更新:



namespace ApartmentFinder.Infrastructure.Models {
    /// <summary>
    /// Provides the set of properties describing a review for a property.
    /// </summary>
    public class Review {
        public string Id{ get; set; }
        public string Name{ get; set; }
        public string Address{ get; set; }
        public string City{ get; set; }
        public string State{ get; set; }
        public string ZipCode{ get; set; }
        public string PhoneNumber{ get; set; }
        public string RatingImageUrl{ get; set; }
        public int NumberOfReviews{ get; set; }
        public string Snippet{ get; set; }
        public string Url{ get; set; }
        public double Latitude{ get; set; }
        public double Longitude{ get; set; }
    }
}


添加用于评论MapYelpBusinessToReview的方法:

    /// <summary>
    /// Maps a <see cref="Y.Data.Business"/> instance to a new <see cref="Review"/>.
    /// </summary>
    /// <param name="business">Business object to map.</param>
    /// <returns>A new <see cref="Review"/> instance.</returns>
    static Review MapYelpBusinessToReview(Y.Data.Business business) {
        return new Review {
            Id = business.id,
            Name = business.name,
            Address = business.location.address[0],
            City = business.location.city,
            State = business.location.state_code,
            ZipCode = business.location.postal_code,
            PhoneNumber = business.phone,
            RatingImageUrl = business.rating_img_url,
            Snippet = business.snippet_text,
            Url = business.url,
            NumberOfReviews = business.review_count,

        };
    }

最佳答案

问题在于,在SelectMany调用中,您最终返回的是null值,而不是空集合。如果您不希望添加任何值,则必须返回一个空集合,否则SelectMany将为空ref

尝试以下

return tasks
  .SelectMany(t => t.Result.businesses ?? new List<Business>())
  .Select(MapYelpBusinessToReview);

关于c# - 未将对象引用设置为对象的实例:Yelp返回值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22152721/

10-11 08:28