0

I'm trying to read from a text file in C# and assign each line to specific variables but I get Unhandled Exception: System.ArgumentNullException: Value cannot be null. Error when I try to convert string to SongGenre.

public static void LoadSongs(string fileName)
{
    string title = "a";
    string artist;
    string length;
    string genre;
    TextReader reader = new StreamReader(fileName);
    while (title != null)
    {
        title = reader.ReadLine();
        artist = reader.ReadLine();
        length = reader.ReadLine();
        genre = reader.ReadLine();
        songs.Add(new Song(title, 
                           artist, 
                           Convert.ToDouble(length), 
                           (SongGenre)Enum.Parse(typeof(SongGenre), 
                           genre)));
    }
    reader.Close();
}

file name refers to a text file which is ok. I tried to it outside a loop like this:

TextReader reader = new StreamReader(fileName);
string title = reader.ReadLine();
string artist = reader.ReadLine();
double length = Convert.ToDouble(reader.ReadLine());
SongGenre genre = (SongGenre)Enum.Parse(typeof(SongGenre), 
                                        reader.ReadLine());
songs.Add(new Song(title, artist, length, genre));

string title1 = reader.ReadLine();
string artist1 = reader.ReadLine();
double length1 = Convert.ToDouble(reader.ReadLine());
SongGenre genre1 = (SongGenre)Enum.Parse(typeof(SongGenre), 
                                         reader.ReadLine());
songs.Add(new Song(title1, artist1, length1, genre1));

and it worked. I also tried to print genre using Console.WriteLine and it was working so I made sure that it is no null but I don't know why am I receiving this error.

It is not a duplicate of other questions. The loop is not working correctly and below you can find how to fix it.

Adrien
  • 3
  • 3
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Cee McSharpface Oct 09 '19 at 07:11
  • Could you indicate the instruction that causes the exception, please? –  Oct 09 '19 at 07:14
  • 2
    Please post a complete exception with stack. I guess it happens in `Enum.Parse`. In any case it would be hard to help without [mcve]. Your code can be optimized, but looks fine to me. The error is probably due to input file, [ReadLine](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader.readline) can return `null` if "the end of the input stream is reached". – Sinatr Oct 09 '19 at 07:15
  • You should check if 'genre' in text file is not missing for one of the entries. I think it's the only reason why `Enum.Parse` would throw ArgumentNullException. – Alexander Goldabin Oct 09 '19 at 07:36

1 Answers1

1

You are getting the exception because of how you set up your loop. You are checking title too early. For the very last iteration all your variables will be null, and you get an exception when trying to parse a null string. You need something like this.

while (true)
{
    title = reader.ReadLine();
    if (title == null) break;
    artist = reader.ReadLine();
    length = reader.ReadLine();
    genre = reader.ReadLine();
    songs.Add(new Song(title, 
                       artist, 
                       Convert.ToDouble(length), 
                       (SongGenre)Enum.Parse(typeof(SongGenre), 
                       genre)));
}

This example is not complete, as you should probably handle all sort of issues with your input file. (e.g. what if there aren't enough lines in the file, or if the lines you are parsing don't contain the right type of data)

To discover such issues by yourself you should look into debugging.

Tiberiu Maran
  • 1,983
  • 16
  • 23