none
Unable to populate list from another list inside a list RRS feed

  • Question

  • Hello,

    I have an C#, Asp.Net, Core 2.0, MVC application developed in Visual Studio 2017.

    When I execute this code:

              public async Task<List<Product>> GetProductListAPIAsync(string serverUrl, string serverKey, string queryCall, List<Category> categories, CancellationToken cancelToken = default(CancellationToken))
              {
                   var uri = Util.processServiceUri(serverUrl, queryCall);
                   KeyAndParametersProductList keyAndParameters = new KeyAndParametersProductList() { key = serverKey };
                   foreach (Category cat in categories)
                   {
                        foreach (SubCategory subcat in cat.subcategories)
                        {
                             keyAndParameters.categories.Add(subcat.name);
                        }
                   }
    
                   var json = JsonConvert.SerializeObject(keyAndParameters);
                   var response = await Request(HttpMethod.Post, uri, json, new Dictionary<string, string>());
                   string responseText = await response.Content.ReadAsStringAsync();
                   var serializedResult = JsonConvert.DeserializeObject<Products>(responseText);
    
                   return (serializedResult.products);
              }


    It gives the error "NullReferenceException: Object reference not set to an instance of an object." on this line: keyAndParameters.categories.Add(subcat.name);

    Here are the Models for the code:

    namespace ChinavasionAPI.Models
    {
    
         public class Categories
         {
              public List<Category> categories { get; set; }
         }
    
         public class Category
         {
              [Display(Name = "Category Name")]
              public string name { get; set; }
              public string url { get; set; }
              public string image { get; set; }
              public string start_content { get; set; }
              public string finish_content { get; set; }
              public List<SubCategory> subcategories { get; set; }
         }
    
         public class SubCategory
         {
              public string name { get; set; }
              public string url { get; set; }
              public string image { get; set; }
              public string start_content { get; set; }
              public string finish_content { get; set; }
         }
    }

    using System.Collections.Generic;
    
    namespace ChinavasionAPI.Models
    {
         public class KeyAndParametersProductList
         {
              public string key { get; set; }
              public List<string> categories { get; set; }
         }
    }

    I want to populate the KeyAndParametersProductList.categories from the public List<SubCategory> subcategories { get; set; }.

    I can't figure out how to code it to work.

    Thanks,
    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.



    • Edited by Carneno Saturday, July 7, 2018 4:05 PM
    Saturday, July 7, 2018 3:00 PM

Answers

  • You did not initialize the categories property. Use either lazy initailazion or ctor:

    // Lazy intialization.
    public class KeyAndParametersProductList
    {
        private List<string> categories;
        public string Key { get; set; }
        public List<string> Categories
        {
            get
            {
                if (this.categories == null)
                {
                    this.categories = new List<string>();
                }
    
                return this.categories;
            }
        }
    }
    
    // ctor,
    public class KeyAndParametersProductList
    {
        public string Key { get; set; }
        public List<string> Categories { get; private set; }
        public KeyAndParametersProductList()
        {
            this.categories = new List<string>();
        }
    }

    • Marked as answer by Carneno Saturday, July 7, 2018 5:05 PM
    Saturday, July 7, 2018 4:39 PM

All replies

  • You did not initialize the categories property. Use either lazy initailazion or ctor:

    // Lazy intialization.
    public class KeyAndParametersProductList
    {
        private List<string> categories;
        public string Key { get; set; }
        public List<string> Categories
        {
            get
            {
                if (this.categories == null)
                {
                    this.categories = new List<string>();
                }
    
                return this.categories;
            }
        }
    }
    
    // ctor,
    public class KeyAndParametersProductList
    {
        public string Key { get; set; }
        public List<string> Categories { get; private set; }
        public KeyAndParametersProductList()
        {
            this.categories = new List<string>();
        }
    }

    • Marked as answer by Carneno Saturday, July 7, 2018 5:05 PM
    Saturday, July 7, 2018 4:39 PM
  • Stefan,

    That worked for me.  I used the ctor initialization.

    Thanks for your help.

    Tony


    Stop The World, I want To Get Off! ........... Life Isn't About Waiting For The Storm To Pass ... It's About Learning To Dance In The Rain.

    Saturday, July 7, 2018 5:05 PM