Results 1 to 5 of 5

Thread: NullReferenceException with StreamReader.BaseStream.Position/.Length/.CanSeek SOLVED

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    NullReferenceException with StreamReader.BaseStream.Position/.Length/.CanSeek SOLVED

    This piece of code is launched as a Thread and runs at the background of my application. Every 3 seconds it copies a file (because it's locked by another application i can only access it after copying it).
    And after that I read all lines that are added to the copied file since the last time I copied it.

    Everytime when i close the file I keep track of the position where my cursor was at that moment. Next time I only need to read starting from that position. (I use a static string filepos for this. And at the start of my application its value is set to 0)

    All new lines are add to an arraylist to be used in other Threads.


    Code:
    		static private void inputloop()
    		{
    			string invoer="";		
    			do
    			{
    				FileInfo bestand = new FileInfo(@"out.txt");
    				if (bestand.Exists)
    				{
    					bestand.CopyTo(@"out2.txt",true);
    					FileInfo bestand2 = new FileInfo(@"out2.txt");
    					StreamReader sr = new StreamReader(bestand2.OpenText());
    					sr.BaseStream.Position = filepos;
    					bool einde = false;
    
    					while (!einde)
    					{
    						try
    						{
    							invoer = sr.ReadLine();
    						}
    						catch
    						{
    							invoer = null;
    						}
    						try
    						{
    							if (invoer.Substring(0,1) == "=") //only lines starting with a "="-sign should be added
    							{
    								inputList.Add(invoer); //added to the list
    							}
    						}
    						catch{}
    								
    							if (invoer == null) 
    							{
    								einde = true;//eof
    							}
    						if (sr.BaseStream.CanSeek) // <----------------- error here
    						{
    							filepos = sr.BaseStream.Length; 
    				
    						}
    						sr.Close();
    					}
    				}
    		System.Threading.Thread.Sleep(3000);
    			}while (true);
    		}
    This looks ok imho. Maybe some things could be done better but it should work, no ? But it doesn't! I get an exception while running it.
    Line 106: corresponds to the BaseStream.CanSeek property I'm using. If I leave it away I get the same mistake but for the BaseStream.Length property this time. I also tried with BaseStream.Position. Same error ...

    Unhandled Exception: System.NullReferenceException: Object reference not set to
    an instance of an object.
    at gnogo2irc.gnugo2irc.inputloop() in c:\gnogo2irc\gnugo2irc.cs:line 106

    Who can help ?
    Last edited by BramVandenbon; Apr 6th, 2004 at 02:44 PM.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502
    The Reason might be that ...

    The BaseStream refers to the Stream Class containing this StreamReader. In this case I didn't get my streamreader from a stream class. I tought that would be no problem. But maybe this means that the basestream doesn't really exist.

    But if this is true it's still strange that I can set the value of Basestream.Position to 0.

    Help is still appreciated ... ?!
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  3. #3
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    what does
    Code:
    FileInfo bestand = new FileInfo(@"out.txt");
    do? I mean what is that @"out.txt"? because it sounds like it isn'r opening anything. Also why not try
    Code:
    if(System.IO.File.Exists("filepath"){ _stream = System.IO.File.Open("filepath",System.IO.FileMode.Open);}}
    Thats my general open file norm
    Magiaus

    If I helped give me some points.

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Lightbulb oh

    The bonus to opening that way is lots of things will just take a stream and become that thing like a bitmap for instance..... also you are using the stream anyway so why add the extra overhead of a streamreader?
    Magiaus

    If I helped give me some points.

  5. #5

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502
    thank you for your help
    I just solved my problem about 5 minutes ago.
    But still thank you for answering.

    What i did was about what you said too.
    I tried another way of opening it using a FileStream.

    thank you very much, i think this thread is solved.
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width