Check on null reference

Posted by Neir0 on Stack Overflow See other posts from Stack Overflow or by Neir0
Published on 2010-03-16T01:04:11Z Indexed on 2010/03/16 1:09 UTC
Read the original article Hit count: 347

Filed under:

Hello, i have the follow code:

searchResults.SearchResultCollection.Add(
                new SearchResult()
                    {
                        Header =
                        HttpUtility.HtmlDecode(
                        htmlDocument.DocumentNode
                        .SelectSingleNode(initialXPath + "h3") 
                        .InnerText),
                        Link = HttpUtility.HtmlDecode(
                        htmlDocument.DocumentNode
                        .SelectSingleNode(initialXPath + "div/cite")
                        .InnerText)
                    }
                );

Sometimes htmlDocument.DocumentNode.SelectSingleNode(....) returns null and my application crashed with NullReferenceException. Ofcourse i can write code which check returned value on null reference but then code will be overly verbose. What is graceful way to do that?

© Stack Overflow or respective owner

Related posts about c#