0
votes

I get this error and I don't know how to resolve it.

The stack trace :

[ArgumentNullException: Value cannot be null. Parameter name: source] System.Linq.Enumerable.FirstOrDefault(IEnumerable'1 source, Func'2 predicate) +4358562

eCommerce.Services.BasketService.addToBasket(HttpContextBase httpcontext, Int32 productid, Int32 quantity) in E:\projects\C#\eCommerce\eCommerce.Services\BasketService.cs:51
eCommerce.WebUI.Controllers.HomeController.AddToBasket(Int32 id) in E:\projects\C#\eCommerce\eCommerce.WebUI\Controllers\HomeController.cs:34

This is the code :

public bool addToBasket(HttpContextBase httpcontext, int productid, int quantity)
{
    bool success = true;

    Basket basket = GetBasket(httpcontext);

    // this line throws the error
    BasketItem item = basket.BasketItems.FirstOrDefault(i => i.ProductId == productid);

    if (item == null)
    {
        item = new BasketItem()
            {
                BasketId = basket.BasketId,
                ProductId = productid,
                Quantity = quantity
            };
        basket.BasketItems.Add(item);
    }
    else
    {
        item.Quantity = item.Quantity + quantity;
    }

    baskets.Commit();

    return success;
}

Please help me I have been stuck for a while now

2
Check that BasketItems is not nulluser3559349

2 Answers

1
votes

Always check for null values when dereferencing. Put in a check that GetBasket did not return a null, that basket.basketitems is not null before dereferencing it, etc.

0
votes

Without knowing if GetBasket() always returns a value or not, I'll assume that it might not always return a value so we check for null. Also, basket might not be null but BasketItems might be so let's check if that object has any values.

If both conditions are true, then we go ahead and execute the rest of the code. If not, then we return false. You could also create a message here to log or return to the user about the basket being empty/null.

public bool addToBasket(HttpContextBase httpcontext, int productid, int quantity)
        {
            bool success = true;

            Basket basket = GetBasket(httpcontext);

            // Not sure if GetBasket always returns a value so checking for NULLs 
            if (basket != null && basket.BasketItems != null && basket.BasketItems.Any())
            {
                BasketItem item = basket.BasketItems.FirstOrDefault(i => i.ProductId == productid);

                if (item == null)
                {
                    item = new BasketItem()
                    {
                        BasketId = basket.BasketId,
                        ProductId = productid,
                        Quantity = quantity
                    };
                    basket.BasketItems.Add(item);
                }
                else
                {
                    item.Quantity = item.Quantity + quantity;
                }

                baskets.Commit();

                success = true;
            }
            else
            {
                // Basket is null or BasketItems does not contain any items. 
                // You could return an error message specifying that if needed.
                success = false;
            }

            return success;
        }