0

In my system to register products for customers, I'm having the exception Object reference not set to an instance of an object.
My code:

public class Customer
{
    public string Name { get; set; }
    public List<PurchasedProduct> Products { get; set; }
}

public class PurchasedProduct
{
    public string ProductName { get; set; }
}

public static List<Customer> Customers = new List<Customer>();

private void buttonRegisterProduct_Click(object sender, EventArgs e)
    {
        Customer procli = Customers[customersListBox.SelectedIndex];
        PurchasedProduct procomp = new PurchasedProduct();
        procomp.ProductName = PurchasedProductTextBox.Text;
        procli.Products.Add(procomp);
        PurchasedProductsListBox.Items.Add(procomp.ProductName);
    }

I'm getting the exception Object reference not set to an instance of an object. in this line:

procli.Products.Add(procomp);

I apreciate any help.

EDIT
After a time trying and thinking, I managed by this way:

private void buttonRegisterProduct_Click(object sender, EventArgs e)
{
    Customer procli = Customers[customersListBox.SelectedIndex];
    PurchasedProduct procomp = new PurchasedProduct();
    if(procli.Products == null)
    {
        procli.Products = new List<PurchasedProduct>
        {
            new PurchasedProduct
            {
                ProductName = PurchasedProductTextBox.Text
            }
        };
        PurchasedProductsListBox.Items.Add(procli.Products[0].ProductName);
    }
    else
    {
        procomp.ProductName = PurchasedProductTextBox.Text;
        procli.Products.Add(procomp);
        PurchasedProductsListBox.Items.Add(procomp.ProductName);
    }
}

This worked, but I would like more explanation about this code that I reached.
Anyway, I'd be grateful if someone present a better and more comprehensive solution.

DotNet
  • 59
  • 3
  • 9
  • your `List Products` is not initialized anywhere. Either new it in the `Customer` class or in your click event. – ethorn10 Nov 04 '14 at 20:34
  • Null exceptions are always the same thing. You are trying to access the property of an object that is null. In your case, you index into `Customers` to get `procli` but its `Products` is not initialized, so you cant call `Add`. – crthompson Nov 04 '14 at 20:35
  • @paqogomez humm how about `PurchasedProduct procomp = new PurchasedProduct();`? I thought this would solve the problem. – DotNet Nov 04 '14 at 20:37
  • @ethorn10 Yes, I do not know how to initialize the List with the logic of the code. – DotNet Nov 04 '14 at 20:38
  • No, that doesn't solve it. `procli.Products` is what isn't initialized. You can't add the new `PurchasedProduct` to something (`procli.Products`) without it being initialized. – ethorn10 Nov 04 '14 at 20:38
  • @ethorn10 I thought the problem was because of `procomp`, so is the `procli` instance. I need to initialize this in the actual code but I don't know how. – DotNet Nov 04 '14 at 20:39
  • google how to initialize new objects if you don't know this then why are you programming.. this is quite basic actually – MethodMan Nov 04 '14 at 20:42
  • 1
    Read the duplicate link to this question. It is an EXCELLENT review of all of the ways you can get null exceptions. Your variation is explained well. – crthompson Nov 04 '14 at 20:43
  • @paqogomez After a long time thinking I've reached a point. I have updated the question. Please see my edition and help me to understand my new code that worked, but I do not know if is the best solution. – DotNet Nov 04 '14 at 23:48
  • I'm sure this works, however, I would offer these suggestions. Instead of doing a new in your event handler, move the new to the constructor of `Customers`. Perhaps even in the property, like in my next comment. – crthompson Nov 04 '14 at 23:54
  • `private PurchasedProduct _products; public PurchasedProduct Products { get { if (_products == null) _products = new PurchasedProduct(); return _products; }` – crthompson Nov 04 '14 at 23:55
  • Additionally, it is not very OO to put code in your event handler. Move all that code into a method that can be reused and call it from your event handler. – crthompson Nov 04 '14 at 23:56
  • @paqogomez can you post an answer with this new solution and how to implement in my code? – DotNet Nov 05 '14 at 00:17
  • This question is closed. No new answers can be posted. – crthompson Nov 05 '14 at 14:08

0 Answers0