This project has moved and is read-only. For the latest updates, please go here.

System.NullReferenceException when not referencing null values?


An unhandled exception of type 'System.NullReferenceException' occurred in test.exe
Additional information: Object reference not set to an instance of an object.

public static string FixImgSrc(string sourceString)
            //string n = Regex.Replace(sourceString, @"(.*?)\/([^\/]*?)\/([^\/]*?)$", @"/$2-$3");
            HtmlDocument doc = new HtmlDocument();
            var imgs = doc.DocumentNode.SelectNodes("//img");
            string output = sourceString;
            if (sourceString.Contains("src="))
                foreach (var img in imgs)
                    string orig = img.Attributes["src"].Value;
                    //do replacements on orig to a new string, newsrc
                    int slashIndex = orig.LastIndexOf("/");
                    StringBuilder srcBuilder = new StringBuilder(orig);
                    srcBuilder[slashIndex] = '-';
                    string newSrc = srcBuilder.ToString();
                    slashIndex = newSrc.LastIndexOf("/");
                    newSrc = newSrc.Substring(slashIndex);


                    //set new source
                    img.SetAttributeValue("src", newSrc);
                output = doc.ToString();
            return output;
My locals:

The error seems to indicate that the following line references something that's null?
string orig = img.Attributes["src"].Value;
However, as you can see from my locals, that doesn't... seem to be the case.

Is this a bug?
Have I missed something?
am I just a big giant dork-face?
(Note: I am totally new, please be gentle).

I've honestly been working on this for about 6 hours, and aside from figuring out that I can't replace HTMLAP with regular expressions (and not just because I'm RX illiterate), I've made zero progress.

Please; please, please. Please help. Please?

EDIT: Solved it. Need to remove ".Value" when checking if SRC is a valid attribute of IMG.