discount and gift card boxes==>Possibility Object reference not set to an instance of an object.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Path: \Presentation\Nop.Web\Factories\ShoppingCartModelFactory.cs

//discount and gift card boxes
            model.DiscountBox.Display = _shoppingCartSettings.ShowDiscountBox;
            var discountCouponCodes = _workContext.CurrentCustomer.ParseAppliedDiscountCouponCodes();
            foreach (var couponCode in discountCouponCodes)
            {
                    // if discount is null then get the  exception. It should handled by code.
                var discount = _discountService.GetAllDiscountsForCaching(couponCode: couponCode)
                    .FirstOrDefault(d => d.RequiresCouponCode && _discountService.ValidateDiscount(d, _workContext.CurrentCustomer).IsValid);

                model.DiscountBox.AppliedDiscountsWithCodes.Add(new ShoppingCartModel.DiscountBoxModel.DiscountInfoModel()
                {
                    Id = discount.Id, // exception here
                    CouponCode = discount.CouponCode  // exception here
                });
            }


Exception: When discount is null then get the following exception. It should handled by code.

   at Nop.Web.Factories.ShoppingCartModelFactory.PrepareShoppingCartModel(ShoppingCartModel model, IList`1 cart, Boolean isEditable, Boolean validateCheckoutAttributes, Boolean prepareEstimateShippingIfEnabled, Boolean setEstimateShippingDefaultAddress, Boolean prepareAndDisplayOrderReviewData) in ~\Presentation\Nop.Web\Factories\ShoppingCartModelFactory.cs:line 821
   at Nop.Web.Controllers.ShoppingCartController.Cart() in ~\Presentation\Nop.Web\Controllers\ShoppingCartController.cs:line 1257
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
6 years ago
Hi Sohel,

Thanks a lot for reporting. This issue is known and already fixed
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.