Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a class file exclass.cs which has 2 classes exclass and exMember.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PModels
{
    public class exclass
    {
        public string classId { get; set; }
        public string clientId { get; set; }
        public string clientName { get; set; }
        public string divisionId { get; set; }

        public List<exMember> memberships1 { get; set; } = new List<exMember>();
    }
    public class exMember
    {
      
        public string consumerId { get; set; }
      




    }
}



I want to assign value to consumerID, which is inside exMember class. It is giving NullReference error when I assign value.

What I have tried:

C#
public exclass SearchMember()
       {
           List<exclass> memberInfo = new List<exclass>();
           try
           {
               exclass vm = new exclass();
               vm.classId = "009";
               vm.clientId = "444";
               vm.clientName = "abc";
               vm.divisionId = "008";
                vm.memberships1[0].consumerId = "23456";

               memberInfo.Add(vm);



           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.ToString());
               return null;
           }



       }
Posted
Updated 2-Sep-20 21:22pm

Instead of this:
C#
vm.memberships1[0].consumerId = "23456";


Use this:
C#
vm.memberships1.Add(new exMember(){consumerId="23456"});


Note: memberships1 list has been initiated but it's empty (does not have any element). So, when you try to refer to the first element on the list, it causes ArgumentOutOfRangeException (NOT a NullReferenceException!!!).

BTW: Why clientId and consumerId is type of string? For numbers you should use proper data type!
 
Share this answer
 
v3
Comments
CPallini 3-Sep-20 3:22am    
5.
Maciej Los 3-Sep-20 3:24am    
Thank you, Carlo.
Member 10711706 3-Sep-20 3:36am    
Even this throws System Null Reference Exception
Maciej Los 3-Sep-20 3:41am    
In which line? What is complete error message?

Please, refer this: c# - What is a NullReferenceException, and how do I fix it? - Stack Overflow[^]
Member 10711706 3-Sep-20 3:45am    
vm.memberships1.Add(new exMember(){consumerId="23456"}); for this
You forgot to create an instance of the exMember class.
Change from
Quote:
vm.divisionId = "008";
vm.memberships1[0].consumerId = "23456";

to
C#
vm.divisionId = "008";
vm.memberships1.Add(new exMember());
vm.memberships1[0].consumerId = "23456";
 
Share this answer
 
Comments
Maciej Los 3-Sep-20 3:24am    
Different way to achieve the same ;)
5ed!
Member 10711706 3-Sep-20 3:36am    
Even this throws System Null Reference Exception
F-ES Sitecore 3-Sep-20 7:10am    
You keep saying it throws an exception, that information isn't enough for anyone to help you. You have to say which line threw the exception. The above snippet worked ok for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900