-1

Warning : Customer.Customer() Non-nullable property 'CustomerItemList' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. How to resolve this problem?

class Customer : Inventory
{
    public List<string> CustomerItemList { get; set;} //warning occurred in this line
    
    
    public List<Customer> getCustomerList(){
        List<Customer> customers = new List<Customer>();
        List<string> customerItemList1 = new List<string>();
        List<string> customerItemList2 = new List<string>();
        List<string> customerItemList3 = new List<string>();

        customerItemList1.Add("INDHS");
        customerItemList1.Add("INOPL");
        customerItemList2.Add("INCDS");
        customerItemList2.Add("INWSZ");
        customerItemList3.Add("INOPL");
        customerItemList3.Add("INQAB");
        


        customers.Add(new Customer { CutomerId = "CUABC", CutomerName = "Bala", CustomerItemList = customerItemList1, InventoryQty = 25 });
        customers.Add(new Customer { CutomerId = "CUWDZ", CutomerName = "Manju", CustomerItemList = customerItemList2, InventoryQty = 12 });
        customers.Add(new Customer { CutomerId = "CUQOP", CutomerName = "Chandru", CustomerItemList = customerItemList3, InventoryQty = 10 });

        return customers;
    
    }

}
Kokila B
  • 11
  • 2
  • https://stackoverflow.com/a/69685202/6527049 – Vivek Nuna Oct 23 '22 at 03:35
  • 1
    A) That is not the constructor (there is no constructor shown), B) CustomerItemList is never initialized, C) Seems more than a bit odd for Customer to inherit from Inventory. D) a public List property is odd see: **[Guidelines for Collections](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/guidelines-for-collections)** – Ňɏssa Pøngjǣrdenlarp Oct 23 '22 at 03:37
  • You've not properly assigned `getCustomerList` as the getter for `CustomerItemList`, which means that `CustomerItemList` is never initialized and would return null. – Ken White Oct 23 '22 at 03:41
  • You've not properly assigned the value. The getter for CustomerItemList, which means that CustomerItemList does not get initialized and would indeed return a null value – mathis1337 Oct 23 '22 at 04:52

1 Answers1

0

First, that is not happening in constructor.

Second, you are not properly initializing your CustomerItemList in Customer object. You create a list of customers, then create a new customer and add that to customers list.

Each customer has a list called CustomerItemList which is not initialized.

You can add it with initializing from the existing list you create like below:

  customers.Add(new Customer
        {
            CutomerId = "CUABC", CutomerName = "Bala", CustomerItemList = new List<string>(customerItemList1),
            InventoryQty = 25
        });
curiousBoy
  • 6,334
  • 5
  • 48
  • 56