Hey Guys,
my code is having some technical error at run time. It shows following error
An unhandled exception of type 'System.NullReferenceException' occurred in parserFunction.exe
Additional information: Object reference not set to an instance of an object.
Can you please suggest me the way to correct it?
using System;
using System.IO;
namespace parserFunction
{
public class parserclass
{
public string knob_name;
public string value;
public parserclass()
{
string knob_name=String.Empty;
value = String.Empty;
}
}
public static class Program
{
public static parserclass[] parser = new parserclass[1000];
public static void Main()
{
string filename = @"C:\Program Files\Microsoft Visual Studio 9.0\VC\sampleData.cfg";
int i = 0;
int size;
size = Program.parserModule(ref parser, filename);
} //end of main()
public static int parserModule(ref parserclass[] parser, string FILE_NAME)
{
int size = 0;
int i = 0;
char[] delimiter = { ' ','\t','\n' };
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return -1;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
string[] words=new string[7000];
words = input.Split(delimiter);
foreach (string s in words)
{
//Console.Write(s);
parser[i].knob_name = s;
Console.WriteLine(parser[i].knob_name);
i++;
}
}
size = i ;
}
for(i = 0;i<size;i++)
{
int loc = parser[i].knob_name.IndexOf(" ");
if(loc == -1)
{
parser[i].value="\0";
//cout<<parser[i].knob_name<<" ---------> "<<parser[i].value<<"\n";
Console.Write(parser[i].knob_name);
Console.Write("\n");
}
else
{
parser[i].value=parser[i].knob_name.Substring(loc+1);
parser[i].knob_name =parser[i].knob_name.Substring(0,loc);
Console.Write(parser[i].knob_name);
Console.Write(" ------> ");
Console.Write(parser[i].value);
Console.Write("\n");
}
}
return size;
}
}
}