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.
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);
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.
And here is the exception:
Thanks
Terry Clancy
ClanceZ
Never mind - I found my problem. Here is the corrected code with changes as indicated by comments
Hope this helps
Terry Clancy
ClanceZ
That looks good, sorry no one was able to reply. Please keep asking questions :)
is working on a reply...