我在帮助程序中有以下函数,它在尝试将项添加到NewsItemList **时出错(所有参数都已正确填充)。错误是“对象引用未设置为对象的实例”。据我所知,我已经实例化了newsList对象,应该可以添加到它的NewsItemList。
private NewsListModel GetNewsList()
{
if(_currentPage.Children.Any())
{
NewsListModel newsList = new NewsListModel();
foreach(var item in _currentPage.Children()) {
var title = item.Name;
var image = 0;
var images = GetCarouselSlides(item);
if (images.Count > 0) {
image = images.FirstOrDefault().ImageId;
}
var summary = item.GetPropertyValue("pageContent").ToString().Substring(0,200);
var link = item.Url;
**newsList.NewsItemList.Add(new NewsItemModel(title, image, summary, link));
}
return newsList;
}
else
{
return null;
}
}
NewsListModel是: -
using System.Collections.Generic;
namespace myProject.Library.Models.Content
{
public class NewsListModel
{
public List<NewsItemModel> NewsItemList { get; set; }
public NewsListModel(){}
public NewsListModel(List<NewsItemModel> newsItemList) {
NewsItemList = newsItemList;
}
}
}
NewsItemModel是: -
using System.Collections.Generic;
namespace myProject.Library.Models.Content
{
public class NewsItemModel
{
public string Title { get; set; }
public int Image { get; set; }
public string Summary { get; set; }
public string Link { get; set; }
public NewsItemModel(){}
public NewsItemModel(string title, int image, string summary, string link)
{
Title = title;
Image = image;
Summary = summary;
Link = link;
}
}
}
这位新手会欣赏任何指针。
谢谢。