-1

Here request is the LIST with the "sortcolumns" property, so Initialy it "sortcolumns" is null so programmatically I am trying to assign a two values which are part of sort columns

request.SortColumns.Add( new SortColumn() { Name = "PolicyName", Direction = DirectionType.Descending });

I am always getting this error, please let me know why - Object reference not set to an instance of an object.

here

public class SortColumn
{
    public string Name { get; set; }
    public DirectionType Direction { get; set; }
}
Backs
  • 23,679
  • 4
  • 50
  • 77
Kumas
  • 3
  • 6

2 Answers2

2

You seemingly need to initialize the list before you start adding to it (once)

request.SortColumns = new List<SortColumn>();

This might be in the constructor of the request, it might be before you Add code, you might even make it a intialised property of the class itself

Example of Property Intialization

public class SomeRequest
{

   public List<SortColumn> SortColumns {get;set;} = new List<SortColumn>();

   ...

Example of Constructor Intializer

public class SomeRequest
{

   public SomeRequest()
   {

       SortColumns = new List<SortColumn>();
       ...
TheGeneral
  • 75,627
  • 8
  • 79
  • 119
0

You need to initialise the property before you use it, try this:

request.SortColumns = new List<SortColumn>();
request.SortColumns.Add( new SortColumn() { Name = "PolicyName", Direction = DirectionType.Descending });
Xiaosu
  • 595
  • 1
  • 7
  • 21