Copied to clipboard

Flag this post as spam?

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


  • Zain Ul Hassan 6 posts 96 karma points
    Aug 12, 2018 @ 09:41
    Zain Ul Hassan
    0

    Getting Null reference exception while using Archetype package

    Hi all I am a newbie and using Umbraco version 7.11.1. I integrated Archetype package in my site and got an exception. Now what I want to do is, I want to make a section/widget of RecentArticles on homepage in my site using Archetype. For this, I installed package and enter the fieldset properties, (You can see the pics below). Then I made my model class RecentArticles and then controller. All the code is given below. Important point is I have got the exception before writing the code for using Archetype in razor. I only write code for controller and got exception.

    In fact, I have followed the lecture of an Umbraco MVP Paul Seal. Lecture URL is https://www.youtube.com/watch?v=tabQKn1JZkM&index=7&list=PL90L_HquhD--OWQNLyO1-KRxVDd0NPBfZ . I did the same as he did, he build the code and "build succeeded" while I build and get exception.

    My code of Home controller is as follows:

    public class HomeController : SurfaceController
        {
            private const string Partial_View_Folder_Path = "~/Views/Partials/Home/";
        //RecentArticles
        public ActionResult RenderRecentArticles()
        {
            List<RecentArticles> model = new List<RecentArticles>();
            IPublishedContent homePage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "home").FirstOrDefault();
            ArchetypeModel recentArticles = homePage.GetPropertyValue<ArchetypeModel>("recentArticles");
    
            foreach (ArchetypeFieldsetModel fieldset in recentArticles)
            { 
                int imageId = fieldset.GetValue<int>("image");
                var mediaItem = Umbraco.Media(imageId);
                string imageUrl = mediaItem.Url;
    
                int pageId = fieldset.GetValue<int>("page");
                IPublishedContent linkedToPage = Umbraco.TypedContent(pageId);
                string linkUrl = linkedToPage.Url;
    
                model.Add(new RecentArticles(fieldset.GetValue<string>("intro"), fieldset.GetValue<string>("authorName"),
                    fieldset.GetValue<DateTime>("dateTime"), imageUrl, linkUrl));
    
    
            }
            return PartialView(Partial_View_Folder_Path + "_RecentArticles.cshtml");
            }
    }
    

    And the code of my RecentArticles model class model is:

    public class RecentArticles
    {
        public string Intro { get; set; }
        public string AuthorName { get; set; }
        public DateTime TimeOfPosted { get; set; }
        public string ImageUrl { get; set; }
        public string LinkUrl { get; set; }
    
        public RecentArticles(string intro, string authorName, DateTime timeOfPosted, string imageUrl, string linkUrl)
        {
            Intro = intro;
            AuthorName = authorName;
            TimeOfPosted = timeOfPosted;
            ImageUrl = imageUrl;
            LinkUrl = linkUrl;
        }
    }
    

    And the look and feel of my Archetype in umbraco backoffice is as follows:

    picture of umbraco backoffice

    Then I give fieldset properties in this data type as follows:

    picture of umbraco backoffice

    Finally, When he (Paul Seal) had built the solution to check the existence of errors then he didn’t get any error. While I compile it, I am still getting the following Null reference exception.

    picture of exception

    And when I put the breakpoint on code, I get the same exception of null, you can see below:

    picture after putting breakpoint in visual studio

    Now, what can I do? Why I am getting this null exception even I have added the data in Content section in Umbraco backoffice as depicted above. Any help will be strongly appreciated.

    Zain

  • Ben Palmer 180 posts 866 karma points c-trib
    Aug 12, 2018 @ 17:50
    Ben Palmer
    0

    Hi Zain,

    I don't have an answer to your specific problem right now but I'd strongly suggest looking at replacing the use of Archetype with Nested Content which is now part of Umbraco Core. It provides a lot of benefits over Archetype which has now ceased development.

    I realise it's more to learn but it'll be worth the time investment :)

    Thanks,

    Ben

  • Zain Ul Hassan 6 posts 96 karma points
    Aug 12, 2018 @ 18:24
    Zain Ul Hassan
    0

    Hi Ben

    Thanks for your reply. I will follow your suggestion. :)

    Again thanks,

    Zain

Please Sign in or register to post replies

Write your reply to:

Draft