GetCurrentShoppingCart() throwing NullReferenceException

Brandon Prudent asked on April 18, 2018 16:21

Getting an unexpected NullReferenceException from the ShoppingService.GetCurrentShoppingCart() method. Here's the relevant stacktrace:

[NullReferenceException: Object reference not set to an instance of an object.] CMS.Ecommerce.CurrentShoppingCartService.GetCurrentShoppingCart(UserInfo user, SiteInfoIdentifier site) +40 CMS.Ecommerce.ECommerceContext.get_CurrentShoppingCart() +231 Kentico.Ecommerce.ShoppingService.GetCurrentShoppingCart() +29 Sologic.Website.Current.get_Order() in C:\Sologic\src\sologic.website\Website\Current.cs:36 Sologic.Website.Filters.ViewBagFilter.OnActionExecuting(ActionExecutingContext filterContext) in C:\Sologic\src\sologic.website\Website\Filters\ViewBagFilter.cs:44

The code isn't exotic:

public static Lib.Services.Order Order
{
    get
    {
        if (HttpContext.Current.Session["Order"] == null)
            HttpContext.Current.Session["Order"] = new Lib.Services.Order(Culture, new ShoppingService().GetCurrentShoppingCart());

        return HttpContext.Current.Session["Order"] as Lib.Services.Order;
    }
}

It worked when the user already had a cart object somehow. Is there some cart initialization code that needs to run first? I think because this is called very early on (after application start and before any controller request has processed) there must be some initializer that didn't get a chance to fire. What needs to happen for this code to run successfully? I'd understood .GetCurrentShoppingCart() would create a cart if it didn't exist. There must be some precondition to that?

Recent Answers


Michal Samuhel answered on April 19, 2018 09:12

Hey Brandon,

Do you have your ow implementation of ICurrentShoppingCartService? What version are you on?

I took a look at both v11 and 10 GetCurrentShoppingCart since these are only versions using the service and there is no method which would take 0 arguments in any of those. Both of the default implementations require UserInfo and SiteInfo as a first and second argument to work with.

0 votesVote for this answer Mark as a Correct answer

Brandon Prudent answered on April 19, 2018 15:39

My definition

I'm on v11 and I don't see a method accepting any arguments. I should mention this is an MVC setup, and I'm following the instructions from pages like https://docs.kentico.com/k11/developing-websites/developing-sites-using-asp-net-mvc/developing-on-line-stores-in-mvc/checkout-process-in-mvc/using-a-shopping-cart-on-mvc-sites.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.