Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Criterion null reference when using SelectionFactoryType

Vote:
 

Hi,

I've followed the documentation on creating criterions:

http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/Personalization/Personalization/

However when I try to drop in the new critierion I get an Object reference not set to an instance of an object.-exception.

If I remove the "SelectionFactoryType"-property on the "DojoWidget"-attribute everything works fine.

public class Group: CriterionModelBase
    {
        [Required]
        [DojoWidget(
            WidgetType = "dijit/form/Select",
            SelectionFactoryType = typeof(GroupSelectionFactory))]
        public string GroupName { get; set; }

        public override ICriterionModel Copy()
        {
            return ShallowCopy();
        }
    }
[VisitorGroupCriterion(
        Category = "",
        Description = "",
        DisplayName = "")]
    public class Critierion : CriterionBase
    {
        public override bool IsMatch(IPrincipal principal, HttpContextBase httpContext)
        {
            try
            {
                int groupId;
                if (!int.TryParse(Model.GroupName, out groupId))
                    return false;
                // validation
            }
            catch (Exception ex)
            {
                // logging
                return false;
            }
        }
    }
public class GroupSelectionFactory : ISelectionFactory
    {

        public IEnumerable GetSelections(ExtendedMetadata metadata)
        {
            // Get stuff from db
            return allGroups.Select(group => new SelectItem
            {
                Text = group.GroupName,
                Value = Convert.ToString(group.GroupId)
            });
        }
    }


If I create a custom UI the exception is thrown on Line 3

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 1:  <%@ control language="C#" inherits="System.Web.Mvc.ViewUserControl">
Line 2:  <%@ import namespace="EPiServer.Personalization.VisitorGroups">
Line 3:  <%= html.dojoeditorform=""> Model.GroupName) %>

Attaching full exception details below:

[NullReferenceException: Object reference not set to an instance of an object.]
   EPiServer.Personalization.VisitorGroups.DojoHtmlExtensions.DojoDropDownFor(HtmlHelper`1 html, PropertyInfo p, Object htmlAttributes, String label, String labelCssClass, LabelPosition labelPosition, DojoWidgetAttribute attr) +99
   EPiServer.Personalization.VisitorGroups.DojoHtmlExtensions.DojoEditorFor(HtmlHelper`1 html, PropertyInfo propertyInfo, Object htmlAttributes, String label, String labelCssClass, LabelPosition labelPosition) +241
   EPiServer.Personalization.VisitorGroups.DojoHtmlExtensions.DojoEditorFor(HtmlHelper`1 html, Expression`1 expression, Object htmlAttributes, String label, String labelCssClass) +98
   EPiServer.Personalization.VisitorGroups.DojoHtmlExtensions.DojoEditorFor(HtmlHelper`1 html, Expression`1 expression) +87
   ASP.views_shared_editortemplates_smilergroup_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Projects\Folkspel\WebSites-Web\Src\Folkspel.WebSites.Web\Views\Shared\EditorTemplates\SmilerGroup.ascx:3
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +262
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +79
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +159
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +75
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +79
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +8920

Any leads or clues would be greatly appreciated.
#148617
May 18, 2016 14:46
Vote:
 

Hi,

I'm not sure, but maybe in your GroupSelectionFactory you are using EPiServer.Shell.ObjectEditing.ISelectionFactory, but you should EPiServer.Personalization.VisitorGroups.ISelectionFactory:

public class GroupSelectionFactory : EPiServer.Personalization.VisitorGroups.ISelectionFactory
    {
	
	// you should implement different methd
 /*
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            // Get stuff from db
            return allGroups.Select(group => new SelectItem
            {
                Text = group.GroupName,
                Value = Convert.ToString(group.GroupId)
            });
        }
		*/
		
		public IEnumerable<SelectListItem> GetSelectListItems(Type propertyType)
		{
		 // Get stuff from db
            return allGroups.Select(group => new SelectItem
            {
                Text = group.GroupName,
                Value = Convert.ToString(group.GroupId)
            });
		}
    }
#148626
May 18, 2016 19:16
Vote:
 

Hi, Grzegorz Wiecheć  

And thank you. That was it. The documentation could be clearer on which interface to implement, but now when I see it, it's obvious.

#148631
May 19, 2016 6:52
Vote:
 

One could argue that ISelectionFactory for both interfaces was not the best naming :)

#148633
May 19, 2016 9:24
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.