Click here to Skip to main content
15,568,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,I am making a parser module to read from a file. Can anyone help me why at the runtime, it is giving thie 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.

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.txt";
            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 = { ' ', '\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)
                        continue;             
                    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.WriteLine(parser[i].value);
                        
                    }

                }
            return size;
        }
}

}
Posted
Updated 14-Aug-10 21:55pm
v3
Comments
Keith Barrow 15-Aug-10 3:19am    
I had to compile your code, if you run under debug, the IDE stops at the incorrect line of code.

You instantiate the parser array:
public static parserclass[] parser = new parserclass[1000];

but you never instantiate the items inside the class:
for (int i = 0; i < parser.Length; i++)
{
   parser[i] = new parserclass()
}


A few other notes:

Your code would be much cleaner if you split the line parsing code from code that iterates over the file. (And break it down into sub-tasks generally.

You will also have cleaner code with a generic List<string></string> and List<parserclass></parserclass>, you can add items to these without declaring fixed sized arrays (that might be too small, and are almost definitely too large). The magic number guess [1000] and [7000] is likely to be wrong, unless you know there are 7000 words per line and 1000 lines exactly per file.

Even if you do not use List<t> you should change
string[] words=new string[7000];
words = input.Split(delimiter);

to
string[] words = input.Split(delimiter);


Namespaces and classnames should be in CamelCase in .net.
 
Share this answer
 
Well,Object reference not set to an instance of an object simply means you are trying to use an object that has a value NULL.

Use DEBUGGER, and get to the line where the error is and just have a NULL check.
If you had posted small code-snippet or pointed out the line then we might had tried to suggested which object that can be.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900