locked
C# Initializing a List in Constructor RRS feed

  • Question

  • User-971051533 posted

    I am trying to create a bootstrap dropdown menu that I can populate from a service. When I try to populate it, I am get the following error message:

    An exception of type 'System.NullReferenceException' occurred in App_Web_l2fqjfej.dll but was not handled in user code. Additional information: Object reference not set to an instance of an object.

    I thought that this meant I wasn't initializing my lists in my constructors, but this isn't fixing the problem. I'm getting the error on the line of code category.Attributes.Add(attribute); I'll paste all of my code below, if someone could push me in the right direction that'd be awesome.

    protected void createFilter(int categoryid)
    {
    
         // check cateogyrid 
    
        //get list of proudct id
        List<int> productIds = new List<int>();
        DataRow[] productRow = CategoriesProductsData.Tables["Products"].Select("Category_ID = " + 573);
    
        productIds = productRow.Select(p => int.Parse(p["Product_ID"].ToString())).ToList();
    
        //get attributes
        ITCProductService pService = new TCProductServiceClient();
        var productTuples = (pService.GetProductsAttributes(productIds));
    
        List<Tuple<int, CustomAttribute>> customAttributes = new List<Tuple<int,CustomAttribute>>();
        foreach(var productTuple in productTuples)
        {
            foreach(var attributeTuple in productTuple.m_Item2)
            {
                var customAttribute = new Tuple<int, CustomAttribute>(productTuple.m_Item1, new CustomAttribute(attributeTuple));
                customAttributes.Add(customAttribute);
            }
        }
    
        List<CustomAttributeCategory> categories = new List<CustomAttributeCategory>();
    
        var categoryTuples = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).Distinct();
        foreach (var categoryTuple in categoryTuples)
        {
            var category = new CustomAttributeCategory(categoryTuple);
            var attributeByCategory = customAttributes.Select(a => a.Item2).Where(b => b.Attribute.CategoryId == categoryTuple.AttributeCategoryId).Distinct();
            foreach (var attributeTuple in attributeByCategory)
            {
    
                var attribute = new CustomAttribute(attributeTuple.Attribute);
                var attributeProductIds = customAttributes.Where(a => a.Item2.Attribute.AttributeId == attributeTuple.Attribute.AttributeId).Select(a => a.Item1).ToList();
                attribute.ProductIds = attributeProductIds;
    
    
               category.Attributes.Add(attribute); // I get the error here.                
    
            }
            categories.Add(category);
        }
    public class CustomAttributeCategory
    {
        public AttributeCategoryModel Category { get; set; }
        public List<CustomAttribute> Attributes { get; set; }
    
        public CustomAttributeCategory(AttributeCategoryModel category)
        {
            Category = category;
            List<CustomAttribute> Attributes = new List<CustomAttribute>();
    
        }
    }
    
    public class CustomAttribute
    {
        public AttributeModel Attribute { get; set; }
    
        public List<int> ProductIds { get; set; }
    
        public CustomAttribute(AttributeModel attribute)
        {
            Attribute = attribute;
            List<CustomAttribute> ProductIds = new List<CustomAttribute>();
        }
    }

    Friday, August 14, 2015 3:00 PM

Answers

  • User-971051533 posted

    Never mind.  I figured it out.  The constructors should be written like this:

    public class CustomAttributeCategory
        {
            public AttributeCategoryModel Category { get; set; }
            public List<CustomAttribute> Attributes { get; set; }
    
            public CustomAttributeCategory(AttributeCategoryModel category)
            {
                Category = category;
                Attributes = new List<CustomAttribute>();
    
            }
        }
    
        public class CustomAttribute
        {
            public AttributeModel Attribute { get; set; }
    
            public List<int> ProductIds { get; set; }
    
            public CustomAttribute(AttributeModel attribute)
            {
                Attribute = attribute;
                ProductIds = new List<int>();
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 14, 2015 3:25 PM