0
Reply

streamreader null ref exception

Ryan L

Ryan L

Aug 2 2006 10:32 AM
1.9k
Im banging my head against the wall on this one. Im pretty new to the C# language but Im learning things as I need them. Ive written a simple read a txt file, extract necassary data, and write to new txt file. My problem is that it reads the entire file, extracts everything I tell it to BUT when it reaches the end of the file, it throws a null reference exception on my readline command. I wrote a very similar one yesterday that worked at first and then stopped working and trowing this same error, so i rewrote it today for this new file I have. Anyone have any ideas on how to fix it?

{code}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
FileStream readfile = new FileStream(@"\\fs1\d$\Departments\is\networkadmin\Infosec\reports\AV Audits\172.22audit-080106.txt", FileMode.Open, FileAccess.Read);
FileStream writeFile = new FileStream(@"C:\noAV2.txt", FileMode.Create, FileAccess.Write);
StreamReader read = new StreamReader(readfile);
StreamWriter write = new StreamWriter(writeFile);
string line;
int i = 1;
do
{
line = read.ReadLine().Trim();
if (line.Contains("Unprotected Client"))
{
if (line.EndsWith(".1"))
{
write.WriteLine(line);
Console.WriteLine("Line " + i + " written successfully");
i++;
}
else if (line.EndsWith(".2"))
{
write.WriteLine(line);
Console.WriteLine("Line " + i + " written successfully");
i++;
}
else if (line.EndsWith(".5"))
{
write.WriteLine(line);
Console.WriteLine("Line " + i + " written successfully");
i++;
}
}
}
while (line != null);
write.Close();
read.Close();
readfile.Close();
writeFile.Close();
Console.WriteLine("Finished");
}
}
}
{/code}