0

I am getting an object reference not set even though I am creating a new instance of a list any ideas as to why.

 List<QueryCritera> whereClause = new List<QueryCritera>();
 whereClause=viewConfig.WhereClause;

 foreach (QueryCritera condishion in whereClause)
 {
    string filedname = condishion.fieldName;
    string fieldValue = condishion.Rightvalue;
    string operation = condishion.Operation;
 }
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

2 Answers2

1

Your ViewConfig must be null

if(viewConfig != null)
{
   whereClause=viewConfig.WhereClause;
}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Not necessarily. `viewConfig` could be null, `viewConfig.WhereClause` could be null, or the `viewConfig.WhereClause` list could be *not* null but contain null values. We don't know which, because the OP didn't say in which line the error occurred. He'll have to debug his code. – dcastro Jul 08 '14 at 08:36
1

Your problem is something of the below:

  • the viewConfig is null
  • the viewConfig.WhereClause is null.
  • the viewConfig.WhereClause is not null but contains null values.

In order you find out what of the above is true and act correspondingly, you should debug your code.

though I am creating a new instance of a list

That's true, you create an empty list of QueryCritera objects. However, later you assign to the variable that holds this list, whereClause, the viewConfig.WhereClause, for which something of the things that mentioned above is true and causes the problem.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • Not necessarily. `viewConfig` could be null, `viewConfig.WhereClause` could be null, or the `viewConfig.WhereClause` list could be *not* null but contain null values. We don't know which, because the OP didn't say in which line the error occurred. He'll have to debug his code. – dcastro Jul 08 '14 at 08:37
  • @dcastro thank you for you comment ! I will update my answer correspondingly. – Christos Jul 08 '14 at 08:38