0

I got this issue for whole day and i still couldn't figure it out why.

 public ActionResult AddToCart(string productcode, int productQty)
 {  
     var db = new PetaPoco.Database("ProductDB");
     var sql = Sql.Builder.Append("Select *");
     sql.Append("from Product");
     sql.Append("where Code= @0", productcode);
     Product product = db.SingleOrDefault<Product>(sql);


    Cart cart = GetCart();
    cart.AddToCart(product, productQty);
    return View(cart);

  }

Here is my GetCart() code.

public Cart GetCart()
{
    Cart cart = (Cart)Session["cart"];
    if (cart == null)
    {
        cart = new Cart();
        Session["cart"] = cart;
    }

    return cart;
 }

And here is my AddToCart(Product p, int qty) code.

public List<Product> AddToCart(Product p, int qty)
    {
        if (p != null)
        {
            cart.Add(p);
        }

        return cart;
    }

Error is thrown at cart.Add(p). Does anyone know why is it crying?

jkl
  • 675
  • 2
  • 8
  • 23
  • Where do you define cart in AddToCart? –  Jun 10 '14 at 17:08
  • Can you tell me where you defined cart in your action method? – Mike Cheel Jun 10 '14 at 17:08
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 10 '14 at 17:08
  • Also, when you call AddToCart, your signature requires a type: "Product", you pass a variable named product but you never specify where that variable is defined (your controller only takes a primitive string and int). – xDaevax Jun 10 '14 at 17:19
  • The problem was that i never initialized `List cart` in the class. Thank you all for the comments! – jkl Jun 10 '14 at 17:26
  • 1
    @ChrisPratt I didn't give any down votes...Trust me...Why would i do that... – jkl Jun 10 '14 at 17:28
  • @ChrisPratt and you know what? i don't have enough reputation to give a downvote! :( – jkl Jun 10 '14 at 17:30
  • 1
    @ChrisPratt It wasn't prographer. It was probably someone that thinks we should answer obviously closable questions. – JasonMArcher Jun 10 '14 at 17:47

2 Answers2

0

Try this:

public Cart GetCart()
{

    if (Session["cart"]== null)
    {
        Cart cart = new Cart();
        Session["cart"] = cart;
        return cart;
    }
else
{
Cart cart = (Cart)Session["cart"];
return cart;
}


 }

public List<Product> AddToCart(Product p, int qty)
    {
        if (p != null && cart!=null)
        {
            cart.Add(p);
        }

        return cart;
    }
malkam
  • 2,337
  • 1
  • 14
  • 17
  • That certainly prevents the error and is a valid answer, but the bigger problem is that `cart` is `null` when it shouldn't be. This ironically just sort of makes matters worse by hiding the problem. – Chris Pratt Jun 10 '14 at 17:15
0

Inside the AddToCart the List<Product> instance named cart is still null. Probably it should be initialized at the constructor. You could defend your access to the cart instance code writing

public List<Product> AddToCart(Product p, int qty)
{
    if (cart == null)
        cart = new List<Product>();
    if (p != null)
        cart.Add(p);
    return cart;
}

But, I suppose that this method exists inside the Cart class, so the best place to be sure that your product list is initialized is at the constructor

public class Cart
{
    private List<Product> cart;

    public Cart()
    {
        cart = new List<Product>();
    }
    public List<Product> AddToCart(Product p, int qty)
    {
        if(p != null)
            cart.Add(p);

        return cart;
    }
}

As a finale note, I could suggest a little refactoring. If possible change the name of that variable. It is confusing to have a variable with the same name of the class with just the case different. And the variable cart is also of a different type (a List<Product>)

Steve
  • 213,761
  • 22
  • 232
  • 286
  • yes, you were right! The reason was because `List cart` has never been initialized but i was trying to add Product in it. Thanks a lot! – jkl Jun 10 '14 at 17:24
  • Glad to be of help, now if the downvoter could explain its reasoning perhaps we could complete this answer. – Steve Jun 10 '14 at 17:28
  • I don't have enough reputation to give a downvote...Than how would i give a downvote? – jkl Jun 10 '14 at 17:31
  • @prographer Yes I imagine that is not you. The problem is not the downvote in se, but the reason for the downvote. If something is wrong in an answer, a downvote is well deserved,But it should be accompanied by an explanation – Steve Jun 10 '14 at 17:33