ReadLine()
method of
StreamReader
class
works like, if the
charLen
and
charPos
(private members of the
StreamReader
class) is equal to
0
and
ReadBuffer()
method
0
as well, then
ReadLine()
method
return
null
.
As a result You are getting
null
to
rline
.
So it is clear that we cannt call a method( in this case
Trim(
) ) from a null object.
It might be helpful,
class Program
{
static void Main(string[] args)
{
ReadFromFile("C:\\Temp\\Test2.txt");
}
public static void ReadFromFile(string fileToRead)
{
if (File.Exists(fileToRead))
{
using (StreamReader reader = new StreamReader(fileToRead))
{
string lineOfData = default(string);
if ((lineOfData = reader.ReadLine()) != null)
{
var rr = lineOfData.Trim();
}
}
}
}
}
:)