Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Abhishek Narvekar 4 posts 74 karma points
    Aug 18, 2021 @ 13:34
    Abhishek Narvekar
    0

    Error: Object reference not set to an instance of an object.

    Hello everyone, I am a newbie to umbraco v8. I am trying to create an article list page and In that i am trying to display finite number of articles when the home page loads but I am not really understanding what does this error mean?

    Error: Object reference not set to an instance of an object.

    enter image description here

    here is the code:

    public ArticleResultSet GetLatestArticles(IPublishedContent currentContentItem, HttpRequestBase request)
        {
            var siteRoot = currentContentItem.Root();
            var articleList = GetArticleListPage(siteRoot);
            var articles = articleList.Descendants().Where(x => x.ContentType.Alias == "article" && x.IsVisible()).OrderByDescending(y => y.Value<DateTime>("articleDate"));
            var isArticleListPage = articleList.Id == currentContentItem.Id;
            var fallbackPageSize = isArticleListPage ? 10 : 3;
    
            var pageNumber = QueryStringHelper.GetIntFromQueryString(request, "page", 1);
            var pageSize = QueryStringHelper.GetIntFromQueryString(request, "size", fallbackPageSize);
    
            var pageOfArticles = articles.Skip((pageNumber - 1) * pageSize).Take(pageSize);
    
            var totalItemCount = articles.Count();
            var pageCount = totalItemCount > 0 ? Math.Ceiling((double)totalItemCount / pageSize) : 1;
    
            var resultSet = new ArticleResultSet() { 
                PageCount = pageCount, 
                PageNumber = pageNumber, 
                PageSize = pageSize, 
                Results = pageOfArticles, 
                IsArticleListPage = isArticleListPage,
                Url = articleList.Url
            };
            return resultSet;
        }
    

    can someone please help me?

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    Aug 18, 2021 @ 14:18
    Paul Seal
    0

    Hi

    When you are trying to check the ids of the current content item and the articleList item you are getting the null error.

    It looks like articleList is null. Can you run the site in debug mode and step through those lines to see if a value is set to articleList?

    Kind regards

    Paul

  • Abhishek Narvekar 4 posts 74 karma points
    Aug 18, 2021 @ 15:59
    Abhishek Narvekar
    0

    Hi paul,

    Thanks for the response, I am have been following your umbraco v8 series and its been really fun so far.

    I have managed to solve the error, apprently I need store the result in a variable in method GetArticleListPage(), then return it.

    thanks again for your help!

Please Sign in or register to post replies

Write your reply to:

Draft