Q:

Initialize Nested List

Please refer the below code
List<ProductDM> productDMList = _orderRepo.GetProductList(32, 12);
  for (int i=0;i<productDMList.Count;i++)
        {
            productDMList[i].CabinetList[i].Min = productDMList[i].Min;
            productDMList[i].CabinetList[i].Max = productDMList[i].Max;
        }

public class ProductDM
  {
    public List<InventoryDM> CabinetList { get; set; }
    public double Min { get; set; }
    public double Max { get; set; }
 }
public class InventoryDM
 {
    public Double Min { get; set; }
    public Double Max { get; set; }
 }

The requirement is to loop through the productDMList and bind the returned MIN and MAX values inside the cabinet list. ProductDM fills with MIN and MAX amounts but when assigning those to CabinetList, it returns an error.
This is because the CabinetList is initially empty and it doesn't show the MIN MAX properties in it.

I am using the above code to assign data but returns

Object reference not set to an instance of an object.

because of the CabinetList empty.
How to initialize the cabinet list here???

A:

As Marco Forberg's answer states initializing the CabinetList in the constructor prevents the 

Object reference not set to an instance of an object.

exception.
In addition to this, instead of assigning the Min and Max values via the index accessor:
productDMList[i].CabinetList[i].Min = productDMList[i].Min;
productDMList[i].CabinetList[i].Max = productDMList[i].Max;

You should use the Add() method of the List<> type:
productDMList[i].CabinetList.Add(new InventoryDM { Min = productDMList[i].Min, Max = productDMList[i].Max });

otherwise you will get an 

ArgumentOutOfRangeException

because you try to access an item on the list which doesn't exist yet.

