0

I have the following function in a helper that errors when it tries to add an item to the NewsItemList ** (all the parameters are correctly populated). The error is "Object reference not set to an instance of an object". As far as I know, I've instantiated the newsList object and should be able to add to it's 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;
    }
}

The NewsListModel is:-

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;

        }
    }
}   

The NewsItemModel is:-

    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;
            }
        }
    }  

This novice would appreciate any pointers.

Thank you.

Craig
  • 1,704
  • 1
  • 18
  • 36
  • 2
    `NewsItemList` is null ! – Shyju Oct 30 '17 at 19:17
  • Only if there are no children. If there are it's a NewsListModel. – Craig Oct 30 '17 at 19:53
  • This is not a duplicate question. I've now edited the line that appeared to make people jump on it because NewsList was set to Null at an earlier point in the code. As it is now, post edit, it still gives the same error I described. So if someone would be so kind as to look at my syntax, methodology, I'd really appreciate it, thanks. – Craig Oct 30 '17 at 20:05
  • 1
    Of course its a duplicate - the error means you trying to a the property of a `null` object. And if its being thrown at the `newsList.NewsItemList.Add(...)` line of code then its because `NewsItemList` is `null` (nowhere in you code have you initialized it). You can do it in the default constructor i.e. `public NewsItemModel(){ NewsItemList = new List(); }` or add `newsList.NewsItemList = new List();` before you add any items to it. –  Oct 30 '17 at 21:09
  • Thank you for the excellent "explanation". Which is what I put the question up for. I'm assuming the typo in the first sentence doesn't change the meaning of the whole? I needed the explanation because, funnily enough, I didn't already know the answer. Hopefully it will help others similarly befuddled. Don't worry, after 7+ years, I probably won't be back. Thanks for all the fish. – Craig Oct 31 '17 at 11:35

0 Answers0