Tom
Tom

Reputation: 13

Null reference error in foreach statement

I am getting a null reference error in the below code:

string artistName =     mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationArtist);
string albumName =     mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationAlbum);
string songTitle =     mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationFullName);
foreach (char c in artistName)
{
    artistName = artistName.Replace("?", "");
    artistName = artistName.Replace("*", "");
    artistName = artistName.Replace("/", "");
    artistName = artistName.Replace(":", "");
}
foreach (char c in albumName)
{
    albumName = albumName.Replace("?", "");
    albumName = albumName.Replace("*", "");
    albumName = albumName.Replace("/", "");
    albumName = albumName.Replace(":", "");
}

I have copied this code from another persons project and though I mostly understand what is happening I cannot figure out the error. The error is on the for each (char c in artistName) line. Thanks for any help.

Upvotes: 0

Views: 207

Answers (3)

NeverHopeless
NeverHopeless

Reputation: 11233

C# provides a benefit of coalesce operator ??. Try using like this:

string artistName = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationArtist);
string albumName  = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationAlbum);
string songTitle  = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationFullName);

artistName = Regex.Replace(artistName ?? "" , @"[\*\:\/\?]" , "");
albumName  = Regex.Replace(albumName  ?? "" , @"[\*\:\/\?]" , "");
songTitle  = Regex.Replace(songTitle  ?? "" , @"[\*\:\/\?]" , "");

Upvotes: 0

Damith
Damith

Reputation: 63105

you can add validation before removing special characters like below

artistName= RemoveSymbols(artistName);
albumName= RemoveSymbols(albumName);

private static string RemoveSymbols(string input)
{
    if(!String.IsNullOrEmpty(input))
       return input;

    return  Regex.Replace(input, "[?*/:]", string.Empty);
}

Now you will not get Null reference error but you need to find why you receive null value from mov.get_Annotation method

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503649

Firstly, this code is bad to start with. There's no point in iterating over a string - but then performing the same replacement in it each time.

However, the cause of your problem can only be that artistName is null - it's possible that the other two strings are null too, of course.

You quite possibly want something like:

string artistName = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationArtist);
string albumName = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationAlbum);
string songTitle = mov.get_Annotation((int)QTAnnotationsEnum.qtAnnotationFullName);

artistName = RemoveSymbols(artistName);
albumName = RemoveSymbols(albumName);

...

private static string RemoveSymbols(string input)
{
    if (input == null)
    {
        return input;
    }
    return input.Replace("?", "")
                .Replace("*", "")
                .Replace("/", "")
                .Replace(":", "");
}

You'll still have null references after this if any particular annotation isn't found, but it won't throw an exception. You'll just need to work out what you want to do with those missing values. (For example, you might want to use a hardcoded "Unknown" value, or maybe an empty string.)

Upvotes: 6

Related Questions