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;
        }
}

}

For starters, please don't take this as a personal attack against you... you're just the latest of many people I see doing this of late.

Code Snippet is generally for use in providing 'working code' as example to the community of how to do something. Questions should be kept to the Discussion format instead.

As for your problem itself... the error generated indicates that you are referencing an object that has a null value. This would be either a variable that was not properly declared or a class object that was not properly instantiated.

Looking through your code I'm not specifically seeing the undeclared variable or class on my first pass-over but somewhere in there something is not properly defined or created and that's where the error originates.

yeah, will take care of this.
I found the problem in my code. It didn't have compilation error , but run time error.

This article has been dead for over six months. Start a new discussion instead.