-2

Using this method I am looping through a file removing lines with a 'T' in them and storing them to a list. At the end of the loop it should exit, however it reads the null value and continues to pass it throwing an exception with message...

System.ArgumentNullException: 'Value cannot be null. Parameter name: source'

... at the point where it tries to look for the char.

public static List<string> GetToolsFromFile()
{
    FileStream FS = new FileStream(OpenFile(), FileMode.Open);
    List<string> list = new List<string>();

    using (StreamReader Sr = new StreamReader(FS))
    {
        char c = 'T';

        while (Sr.ReadLine() != null)
        {
            string line = Sr.ReadLine();
            //MessageBox.Show(line.ToString());
            if (line.Contains(c))
            {
                list.Add(line);
                // MessageBox.Show(line.ToString());
            }
        }

        FS.Close();
    }

    return list;
}

The OpenFile() is a method using the OpenFileDialog. This was tested and working as it retrieves and displays the contents of the selected file.

Snoop Dog
  • 11
  • 7
  • The OpenFile() is a method using that. It opens and reads the file. It shows me every line I need to store. When it comes to the end of the file is when it throws the exception. – Snoop Dog Dec 01 '19 at 05:18
  • What if the `OpenFile()` returned `null`? You should check that before you proceed. –  Dec 01 '19 at 05:25
  • JQSOFT if the OpenFile() returned null then how would it read the file I selected? Thanks for the effort but Youssef13 solved it for me. – Snoop Dog Dec 01 '19 at 05:35
  • 2
    @SnoopDog You should not edit the solution into your question. That's what accepting an answer is for, and now the question doesn't make sense for future readers when it presents code that already works. – Lance U. Matthews Dec 01 '19 at 06:00

2 Answers2

1

You're calling the ReadLine method two times. First time is in the while loop condition, and second time is inside the while body. Let's say your file is 3 lines, here are the calls done to ReadLine method:

  1. in loop condition, it reads the first line.
  2. in the loop body, it reads the second line and assigns it to the line variable.
  3. in the loop condition, it reads the third line (which is the last line).
  4. in the loop body, it returns null, so your line variable is assigned to null then you're calling Contains method on a null string. That's what the exception says.
Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • I have tried removing however the first one using != is a bool. If I use while (line = Sr.ReadLine() != null) I get an error stating cannot implicitly convert bool to string. – Snoop Dog Dec 01 '19 at 05:23
  • 1
    @SnoopDog, You need to include parenthesis. `while ((line = Sr.ReadLine()) != null)`. – Youssef13 Dec 01 '19 at 05:27
  • 1
    Adding the extra parenthesis corrected the issue. Thank you for your help – Snoop Dog Dec 01 '19 at 05:31
  • 1
    Dont know why someone down voted it. It fixed the issue correcting it like you stated. Thanks – Snoop Dog Dec 01 '19 at 05:32
  • 1
    +1 for explaining what was wrong and what happens when `ReadLine()` is called like that. It would be good if this either explained or showed how to fix it, although I suppose the two answers together provide code and an explanation. – Lance U. Matthews Dec 01 '19 at 05:33
0

Please see the corrected code

FileStream FS = File.Open(@"c:\path_to_The_File", FileMode.Open);
        List<string> list = new List<string>();

        using (StreamReader Sr = new StreamReader(FS))
        {
            char c = 'T';

            string line = Sr.ReadLine();
            while (line != null)
            {

                //MessageBox.Show(line.ToString());
                if (line.Contains(c))
                {
                    list.Add(line);
                    // MessageBox.Show(line.ToString());
                }

                line = Sr.ReadLine();
            }


            FS.Close();
        }
Soumen Mukherjee
  • 2,953
  • 3
  • 22
  • 34