1

I am using following snippet to some items to a list of strings. But it is throwing an exception.

List<string> guids = null;
QueryExpression qExp = new QueryExpression
{
    EntityName = "account",
    ColumnSet = col1,
    Criteria = new FilterExpression
    {
        Conditions = { 
            new ConditionExpression("statecode",ConditionOperator.Equal,0)
        }
    }
};
sp.CallerId = g1;
EntityCollection ec1 = sp.RetrieveMultiple(qExp);
foreach (Entity item in ec1.Entities)
{
   guids.Add(Convert.ToString(item.Attributes["accountid"]));
}

Exception: Object reference not set to an instance of an object

User089
  • 139
  • 2
  • 13

3 Answers3

2

Change List<string> guids = null; to List<string> guids = new List<string>(); and all will be well.

You must initialise the list before you can start writing to it. You are setting it to null, thus the exception.

David Arno
  • 40,354
  • 15
  • 79
  • 124
2

Why not use LINQ:

List<string> guids = ec1.Entities
   .Select(entity => Convert.ToString(entity.Attributes["accountid"]))
   .ToList();
Magnus
  • 1,022
  • 8
  • 12
1

You cannot use List<string> guids = null;

Try to do List<string> guids = new List<string>();

CBreeze
  • 2,629
  • 3
  • 31
  • 76