Try our conversational search powered by Generative AI!

Custom Line Item Property throws null reference exception

Vote:
 

Hi,

I have custom line item with name TreasuresLineItem

I have below line in ecf.order.config

The class looks like below

  public class TreasuresLineItem : LineItem
    {
        public TreasuresLineItem() : base()
        {
            Initialize();
        }

        public TreasuresLineItem(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            Initialize();
        }

        protected virtual void Initialize()
        {
          
        }

        public bool IsGift
        {
            get { return GetBool(MetaFieldsConstants.LineItem.IsGift); }
            set { this[MetaFieldsConstants.LineItem.IsGift] = value; }
        }

        public string Recipeints
        {
            get { return GetString(MetaFieldsConstants.LineItem.Recipients); }
            set { this[MetaFieldsConstants.LineItem.Recipients] = value; }
        }
}

When i try to add entry to cart and then cast it to TreasureLineItem. The system cast it successfully but accessing properties throws an null exception


/* Need entry for adding to cart */
var entry = CatalogContext.Current.GetCatalogEntry(cartLineItem.Code);
var lineItem = ch.AddEntry(entry, cartLineItem.Quantity, false, new CartHelper[] { }) as TreasuresLineItem;

Throws exception 

lineItem.IsGif accessing property throws exception

I have checked meta field exists in database and it is linked with TreasureLineItem, The database table for extended line item with column also exists.

Do you have any idea why it is happening like this?

#172404
Dec 01, 2016 23:37
Vote:
 

What's Commerce version you're working with?

I'm trying to reproduce your issue but found that IsGift was in LineItem already.

/Son Do

#172413
Edited, Dec 02, 2016 9:24
Vote:
 

Hi @Son Do

I am using version 9.15.0.1605 of Mediachase.Commerce.

ANy custom meta property that i defined in TreasureLineItem throws null exception.

Cheers

#172465
Dec 04, 2016 12:21
Vote:
 

I suppose you want to use the "Strongly typed" approach? Because otherwise you can just add metafields to LineItem and access them like lineItem["YourMetaField"].

The stacktrace of the exception would be helpful.

Regards,

/Q

#172479
Dec 05, 2016 9:15
Vote:
 

Hi @Quan,

Yes, I am trying to use strongle typed approach. I think the mediachase library has some problem. 

The call for adding code to cart AddEntry returns lineitem class for the first time.

e.g call

CartHelper ch = new CartHelper(name);

 var lineItem = ch.AddEntry(entry, cartLineItem.Quantity, false, new CartHelper[] { });

for e.g If the entry code is added for the first time then return type is always LineItem

If you re-add the same code again the return type comes out as TreasuresLineItem.

The reason for this i beleive that Medachase.Commerce uses different way to initialize the line item. I have been debugging and found out that In some places it uses

LineItem lineItem = new LineItem()

and in some places through orderForm it uses 

(LineItem) OrderContext.Current.LineItemClassInfo.CreateInstance();

I think because of these descrepenceies, if given code is added to cart for the first time it returns LineItem and then TreasuresLineItem for the subsequent call for the given code.

Anyway, i still get the null reference exception if i try to access custom property in TreasuresLineItem. Unless i add custom LineItem Class with same name("TreasuresLineItem") and assign same custom property to LineItemEx and newly added CustomLineItem("TreasuresLineItem")

The drawback of this, It adds new tables LineItemEx_TreasuresLineItem and LineItemEx_TreasuresLineItem_Localization but still saves values in LineItemEx.

and if i try to replace ecf.order.config

<MetaClasses><LineItemClass name="TreasuresLineItem"> from LineItemEx in order to save new value into LineItemEx_TreasuresLineItem and LineItemEx_TreasuresLineItem_Localization then returned object from 

ch.AddEntry(entry, cartLineItem.Quantity, false, new CartHelper[] { }) is always of LineItem.

#172514
Edited, Dec 05, 2016 23:49
Vote:
 

Hi,

Can you post the stacktrace of the null reference exception you got?

/Q

#172525
Dec 06, 2016 10:05
Vote:
 

Hi Quan,

I have decided not to inherit from LineItem class instead use in a way defined below

   public class TreasuresLineItem
    {
        private readonly LineItem _lineItem;

        public LineItem LineItem
        {
            get { return _lineItem; }
        }

        public TreasuresLineItem(LineItem lineItem) : base()
        {
            _lineItem = lineItem;
        }


        public bool IsGift
        {
            get { return _lineItem.GetBool(MetaFieldsConstants.LineItem.IsGift); }
            set { _lineItem[MetaFieldsConstants.LineItem.IsGift] = value; }
        }

        public string Recipeints
        {
            get { return _lineItem.GetString(MetaFieldsConstants.LineItem.Recipients); }
            set { _lineItem[MetaFieldsConstants.LineItem.Recipients] = value; }
        }

        public string DeliveryInstructions
        {
            get { return _lineItem.GetString(MetaFieldsConstants.LineItem.DeliveryInstructions); }
            set { _lineItem[MetaFieldsConstants.LineItem.DeliveryInstructions] = value; }
        }

        public string Abstract
        {
            get { return _lineItem.GetString(MetaFieldsConstants.LineItem.LineItemAbstract); }
            set { _lineItem[MetaFieldsConstants.LineItem.LineItemAbstract] = value; }
        }

        public string ImageUrl
        {
            get { return _lineItem.GetString(MetaFieldsConstants.LineItem.ImageUrl); }
            set { _lineItem[MetaFieldsConstants.LineItem.ImageUrl] = value; }
        }

        public MetaDictionaryItem ProductType
        {
            get { return _lineItem.GetDictionaryItem(MetaFieldsConstants.LineItem.ProductType); }
            set { _lineItem[MetaFieldsConstants.LineItem.ProductType] = value; }
        }
    }
#173331
Dec 21, 2016 6:34
* 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.