Hello I am reading two line of data from text file in C#, and at the end of file I get error "Object reference not set to an instance of an object". I know this error is because of end of file, and object is being assigned null value. But i need to avoid this error. My code is in below format:
try
{
sting line;
while ((line = file.ReadLine().Trim()) != null)
{
//do something
if ((line2 = file.ReadLine().Trim()) != null)
//do something
}
}
catch(exception e)
{
console.write(e.Message);
}
At end of file, is where it goes in exception.
Thanks for help in advance.
stinginstead ofstring.console.writeshould be capitalized:Console.Write