Copied to clipboard

Flag this post as spam?

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


  • Terry Clancy 204 posts 944 karma points
    Dec 28, 2015 @ 08:52
    Terry Clancy
    0

    NullReference Exception adding PropertyType to a Member Type

    Hi all,

    I am getting the "NullReferenceException was unhandled by user code" exception shown at the bottom of this post when attempting to add PropertyType to a Member Type. Here is my code and the line causing the exception is marked with <<<<<<<<<<<<< below.

    Any assistance solving this would be VERY much appreciated.

    IDataTypeService  umbDataTypeService = AppHelpers.UmbServices().DataTypeService;
    var allDataTypeDefinitions = umbDataTypeService.GetAllDataTypeDefinitions();
    IDataTypeDefinition labelDataTypeDefinition = allDataTypeDefinitions.FirstOrDefault(dt => dt.PropertyEditorAlias == "Umbraco.Textbox");
    PropertyType resetGUIDPropertyType = new PropertyType(labelDataTypeDefinition)
    
        {
    
        Name = "resetGUID",
    
        Alias = "resetguid",
    
        Key = Guid.NewGuid(),
    
        PropertyEditorAlias = "resetguid",
    
        SortOrder = 0,
    
        Description = "The rain in spain"
    
        };
    MemberType customMemberType = (MemberType) umbMemberService.GetById(customIMemberId);
    customMemberType.AddPropertyType(resetGUIDPropertyType);    // <<<<<<<<<<<< EXCEPTION HAPPENS HERE
    

    And here is the exception:

    System.NullReferenceException was unhandled by user code
    HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=TeraTastic04 StackTrace: at TeraTastic04.TTExtensions.RegisterStartupEvents.AddCustomMemberType() in C:\Clancy\ATeraTastic\Dev\TeraTastic04\TeraTastic04\TTExtensions\RegisterStartupEvents.cs:line 279 at TeraTastic04.TTExtensions.RegisterStartupEvents.ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) in C:\Clancy\ATeraTastic\Dev\TeraTastic04\TeraTastic04\TTExtensions\RegisterStartupEvents.cs:line 88 at Umbraco.Core.ApplicationEventHandler.OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) at Umbraco.Core.CoreBootManager.

    Thanks

    Terry Clancy

    ClanceZ

  • Terry Clancy 204 posts 944 karma points
    Dec 28, 2015 @ 18:55
    Terry Clancy
    100

    Never mind - I found my problem. Here is the corrected code with changes as indicated by comments

    IDataTypeService  umbDataTypeService = AppHelpers.UmbServices().DataTypeService;
    var umbMemberTypeService = AppHelpers.UmbServices().MemberTypeService;    //  Had to add this line
    var allDataTypeDefinitions = umbDataTypeService.GetAllDataTypeDefinitions();
    IDataTypeDefinition labelDataTypeDefinition = allDataTypeDefinitions.FirstOrDefault(dt => dt.PropertyEditorAlias == "Umbraco.Textbox");
    PropertyType resetGUIDPropertyType = new PropertyType(labelDataTypeDefinition)
    
        {
    
        Name = "resetGUID",
    
        Alias = "resetguid",
    
        Key = Guid.NewGuid(),
    
        PropertyEditorAlias = "resetguid",
    
        SortOrder = 0,
    
        Description = "The rain in spain"
    
        };
    //  MemberType customMemberType = (MemberType) umbMemberService.GetById(customIMemberId);   //  ERROR  this line was and is  wrong, resulted in customMemberType being null
    MemberType customMemberType = (MemberType)umbMemberTypeService.GetAll(customIMemberTypeId).FirstOrDefault();  //  This is the corrected line
    customMemberType.AddPropertyType(resetGUIDPropertyType);
    

    Hope this helps

    Terry Clancy

    ClanceZ

  • Charles Afford 1163 posts 1709 karma points
    Dec 30, 2015 @ 20:42
    Charles Afford
    0

    That looks good, sorry no one was able to reply. Please keep asking questions :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies