This is the code to find the product names using keyword. I am getting "Null referrence exception" please resolve this problem. I am getting exception in if statement.
static void Main(string[] args)
{
try
{
int n;
string[] item = null;
string[] productName = new string[6];
string word;
int i;
bool flag = false;
Console.WriteLine("enter the no of products");
n = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < n; i++)
{
productName[i] = Console.ReadLine();
}
Console.WriteLine("enter the keyword to search");
word = Console.ReadLine();
foreach (string item1 in productName)
{
if (item1.Contains(word)) //NullReferenceException
{
flag = true;
item = item1.Split(' ');
Console.WriteLine("item:" + item[0]);
}
}
if (!flag)
{
Console.WriteLine("not found");
Console.ReadLine();
}
}
catch (NullReferenceException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
is there any other way to code this..?