none
"Object reference not set to an instance of an object" while attempting to read a file = why?

    Question

  • Hello guys, first post here (yay!).
    This may be a noobish question, but I've just recently began programming in C#, so please bear with me on this.
    I've encountered a bug in my project while attempting to open two distinct files with FileStreams.

    I'm getting the following error:

    Unhandled Exception: System.NullReferenceException: Object reference not set to
    an instance of an object.
       at DBSourceOrder.Program.Main(String[] args)

    Here's my code:

    namespace DBSourceOrder 
        class Program 
        { 
            static void Main(string[] args) 
            { 
     
                int i = 0
                string linha = ""
                bool skip = false
                int novas_entradas = 0
                StreamWriter MyStreamWriter; 
                FileStream db_cvs_fs = null
                FileStream uni_fs = null
                StreamReader db_cvs_sr = null
                StreamReader uni_sr = null
     
                /* Bad syntax; wrong arguments */ 
                if (args.Length != 3) 
                { 
                    Console.WriteLine("Syntax: <DB CVS> <Sources list> <Output file>"); 
                    Console.Read(); 
                    Environment.Exit(0); 
                } 
                 
                 
                    Console.WriteLine("DBMatcher v1.0"); 
                    Console.WriteLine("Attempting to read files: " + args[0] + " " + args[1]); 
     
                    try 
                    { 
     
                        db_cvs_fs = new FileStream(args[0], FileMode.Open, FileAccess.Read); 
                   //     db_cvs_sr = new StreamReader(db_cvs_fs); 
                    } 
                    catch (IOException e) 
                    { 
                        Console.WriteLine("Bad source file or file not found: " + e); 
                        Console.Read(); 
                        Environment.Exit(0); 
                    } 
     
                    try 
                    { 
                        uni_fs = new FileStream(args[1], FileMode.Open, FileAccess.Read); 
                     //   uni_sr = new StreamReader(uni_fs); 
                    } 
                    catch (IOException e) 
                    { 
                        Console.WriteLine("Bad source file or file not found:  " + e); 
                        Console.Read(); 
                        Environment.Exit(0); 
                    }

                    (...) Commented the rest of the program and left only the following lines:

                /****************************************************************/
                /* Part 5 - Cleanup!                                            */
                /****************************************************************/

                db_cvs_fs.Close();
                db_cvs_sr.Close();
                //uni_sr.Close();
                //uni_fs.Close();
                //Console.ReadLine();



    Now, if I comment the last two uncommented lines (db_cvs_fs.Close() and db_cvs_sr.Close()) the program will not crash.

    However, shouldn't these lines be there? Should I not close the FileStreams?
    Moreover, I've already used this code on previous projects and never got any problems with it...

    Thanks in advanced,
    Hal

    PS: Using Visual Studio 2008 in Windows Server 2008
    • Edited by Knight4 Monday, October 06, 2008 10:15 AM
    Monday, October 06, 2008 10:13 AM

Answers

All replies