Click here to Skip to main content
15,915,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have this code and the third line is where the error is generated:

XML
StringBuilder sbu = new StringBuilder();
            sbu.Append("<div id='content-wrapper'>");
            _ContentInfo.SubCategorySEOName = Page.RouteData.Values["subcategorySEOName"].ToString() != null ? Page.RouteData.Values["subcategorySEOName"].ToString() : null;
            List<ContentInfo> ContentList = _ContentInfo.GetContentBySubCategorySEOName().ToList<ContentInfo>();
            //foreach(
            sbu.Append("<div id='first-wrapper'>");
            foreach (ContentInfo content in ContentList)
            {
                sbu.Append("<h1>" + content.ContentHeading + "</h1>");
                sbu.Append("<p>" + content.Content + "</p>");
            }
            sbu.Append("</div>");
            ltlcontent.Text = sbu.ToString();
            sbu.Append("<div id='second-wrapper'>");

            sbu.Append("</div>");
            sbu.Append("</div>");


I am trying to catch the "Object reference not set to an instance of an object" like this:
_ContentInfo.SubCategorySEOName = Page.RouteData.Values["subcategorySEOName"].ToString() != null ? Page.RouteData.Values["subcategorySEOName"].ToString() : null;

from the third line of the above code.But am still getting the error please can someone help resolve this.
Posted

Well... the output from ToString can never be null - it can be an empty string, but it can't be null, so you test will either throw an exception or always return the value - it is thus worthless.
Even in it could, it boil down to:
C#
x = a != null ? a : null;

Which mean this it always returns the value of "a" anyway: if it's null, it sets "x" to null, if it isn't it sets "x" to the value of "a".

So...you problem is somewhere in here:
Page.RouteData.Values["subcategorySEOName"]

And we can't tell which from here: we don't have access to your code running.

But basically, what is happening is that one of the elements to the left of a "." is null, or the whole thing is, in which case the attempt to convert it to a string with ToString will throw the null reference exception.

Put a breakpoint on the line above the one giving the error and use the mouse to hover over each part in turn - find which one is null and you can start looking for why your code has let it be null.

Sorry, but it's over to you and the debugger, I'm afraid.
 
Share this answer
 
Page.RouteData.Values["subcategorySEOName"].ToString() != null ?Page.RouteData.Values["subcategorySEOName"].ToString() : null

Instead try
Page.RouteData.Values["subcategorySEOName"] != null ? Page.RouteData.Values["subcategorySEOName"].ToString() : null;

If you convert a null value to a string, you will get an error.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900