Anoushka Seechurn
Anoushka Seechurn

Reputation: 2256

Inserting items in a List<>

I am trying to get all the words of a user built query into a List<>.

I did

if (columns[(a - 1)].Contains("$"))
{
    string[] splt1 = rows[b].Split('$');
    string userBuiltQuery = columns[(a - 1)].ToString();
    userBuiltQuery= userBuiltQuery.Replace("$", "");
    var pattern = new Regex(@"\w(?<!\d)[\w'-]*");
    foreach (Match m in pattern.Matches(userBuiltQuery))
    {
        words.Add(Convert.ToString(m));
    }
}
}

But in the foreach loop I get an error saying.

Object reference not set to an instance of an object.

Why so ?

Upvotes: 0

Views: 83

Answers (1)

David Arno
David Arno

Reputation: 43254

Try initialising words to an instance of List<string> before adding elements to it.

Upvotes: 6

Related Questions