Pratik Singhal
Pratik Singhal

Reputation: 6492

Null Reference Exception while iterating over StreamReader List

I want to find a particular string inside 40-50 long files. To do this , I used the following code:-

    foreach(StreamReader SR in FileList)
    {
       // Process the file
    }
    // File List contains initialized instances of StreamReader class

while doing so I am receiving

       null reference exception

although, when the

    FileList 

contains only 1 element the code is working fine. What can be the possible reason for this and how can it be corrected? I have made a function like this, which initializes the files and adds them to FileList:

    public static void Initialize()
    {
      StreamReader File1 = new StreamReader("some valid path here",false, Encoding.UTF8) ; 
       FileList.Add(File1) ; 
       // Similarly for other files.
    } 

the code inside the foreach loop is:-

    foreach( StreamReader SR in FileList)
    {
      while (!SR.EndOfStream)
      {
          Content = SR.ReadLine() ; 
          if(Content.Contains(Name))
          {
           Console.WriteLine("Found");
          }
       }
     } 
      // Content and Name are other string variable declared previously in the program

As some people pointed out that error might be caused by the variable Content, I want to clarify that this is not the case.

Upvotes: 0

Views: 294

Answers (4)

VladL
VladL

Reputation: 13033

There is a very nice and safe method File.ReadLines (don't confuse with File.ReadAllLines)

It will not read the whole content of the file, but will load a line per call

foreach (string path in paths)
{
    foreach (string line in File.ReadLines(path))
    {
        if (line.Contains(Name))
        {
            Console.WriteLine("found");
            break;
        }
    }
}

Upvotes: 1

abto
abto

Reputation: 1628

Check your Content variable for null, because it's likely that SR.EndOfStream is not set correctly if SR is empty and not read to.

If it is possible, that there are null entries in the FileList, check SR for null also.

foreach( StreamReader SR in FileList)
{
  if(SR == null) continue;

  while (!SR.EndOfStream)
  {
      Content = SR.ReadLine() ; 
      if(Content != null && Content.Contains(Name))
      {
       Console.WriteLine("Found");
      }
   }
 } 

Upvotes: 1

Rune FS
Rune FS

Reputation: 21742

From the documentation of StreamReader

The next line from the input stream, or null if the end of the input stream is reached.

So you are reading pass the end of the stream which sets Content to null.

You should therefor change your looping logic

    while ((Content = SR.ReadLine()) != null)
    {
        if (Content.Contains(Name))
        {
            Console.WriteLine("Found");
        }
    }

However I'd suggest to do it rather different

var paths = //set to a list of file paths
var findings = from path in paths
               from line in System.IO.File.ReadAllLines(path)
               where line.Contains(name)
               select line

This will give you all the lines that contains the string in name

Upvotes: 1

decho
decho

Reputation: 331

Prabably because FileList is null and doesn't contains any elements. And of course this will throw exception. You should check that this is not null before to call foreach. .. Or just init somewhere FileList with new before to fill it.

Upvotes: 1

Related Questions