0

I have a helper class, which was working fine in EpiServer before we added an additional site to our instance. Now that we have added an additional site, the helper is throwing a null reference because the new Startpage does not have any InsightsConfiguration.CoreStoriesConfig.CoreStories.

I am trying to rewrite the code below to account for this null reference:

using EPiServer;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Web;
using System.Collections.Generic;

namespace Digital.Site.Helpers.Selections
{
    public class DataLayerCoreStorySelectionFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {

            var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
            var startPage = loader.Get<Models.Pages.StartPage>(SiteDefinition.Current.StartPage);

            var coreStories = startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories;
            ISelectItem[] selectItems = new ISelectItem[coreStories.Count];

            int i = 0;
            foreach (var story in coreStories) {
                selectItems[i++] = new SelectItem() { Text = story, Value = story};
            }

            return selectItems;
        }
    }
}

I have tried the following:

using EPiServer;
using EPiServer.Shell.ObjectEditing;
using EPiServer.Web;
using System;
using System.Collections.Generic;

namespace Digital.Site.Helpers.Selections
{
    public class DataLayerCoreStorySelectionFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {

            var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
            var startPage = loader.Get<Models.Pages.StartPage>(SiteDefinition.Current.StartPage);

            if (startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories != null)
            {
                var coreStories = startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories;
                ISelectItem[] selectItems = new ISelectItem[coreStories.Count];

                int i = 0;
                foreach (var story in coreStories)
                {
                    selectItems[i++] = new SelectItem() { Text = story, Value = story };
                }

                return selectItems;
            }
            
        }
    }
}

But this throws the following error that I am unsure how to get around:

'DataLayerCoreStorySelectionFactory.GetSelections(ExtendedMetadata)': not all code paths return a value

BrokenCode
  • 951
  • 4
  • 19
  • 43

1 Answers1

1
    public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
    {

        var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
        var startPage = loader.Get<Models.Pages.StartPage>(SiteDefinition.Current.StartPage);

        if (startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories != null)
        {
            var coreStories = startPage.InsightsConfiguration.CoreStoriesConfig.CoreStories;
            ISelectItem[] selectItems = new ISelectItem[coreStories.Count];

            int i = 0;
            foreach (var story in coreStories)
            {
                selectItems[i++] = new SelectItem() { Text = story, Value = story };
            }

            return selectItems; // first code path
        }
        // return null; // second path
        throw new CustomException("CoreStories is null"); // if you expect it in the if
    }

What the error basically says is that your code has more than one path. And in your case you fork the path with the if. Just imagine what happens when to condition resolves to false, what shall be returned in this case? And saying "this won't happen" is not correct as you won't need an if in this case.

Every if has either an explicit else you can write, or an implicit "else" that occurs when the condition resolves to false

SirOneOfMany
  • 929
  • 5
  • 15
  • Thank you very much. I would like to return an empty selectItems in cases where a Startpage does not have any core stories, I guess I could return null simply? – BrokenCode Sep 06 '21 at 11:25
  • Well it depends. null is not an empty list. You could either return null and check for null or return a new selectItems object without content. This is something different – SirOneOfMany Sep 06 '21 at 11:35