0

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.

4
  • 6
    artistName is null. Commented Aug 13, 2013 at 5:49
  • 1
    What do you see when you put a breakpoint on string artistName = ...? Commented Aug 13, 2013 at 5:50
  • I know but how would I do this so artist name isnt null? I basically need to remove illegal characters to use the strings in a directory. I am in way over my head making this program but I kinda need it. Commented Aug 13, 2013 at 5:51
  • Have a look at mine's as well Commented Aug 13, 2013 at 6:42

3 Answers 3

6

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks its stopped the error, unfortunately now my program freezes with no errors :/
@Tom: I suggest you work on diagnosing why that is, and then ask a new question with appropriate details if you get stuck.
0

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

Comments

0

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  ?? "" , @"[\*\:\/\?]" , "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.