I am getting "object reference error" while putting string.format in stringbuilder. I have below code in C#2.0
public const string PageLinkGoogleMarkup = "<link rel=\"alternate\" hreflang=\"{0}\" href=\"{1}www.mysite.com{2}{3}\" />\r\n";
Now when I am adding this to HtmlTextWriter it is working fine, as below:
HtmlTextWriter writer (Object);
writer.write(string.format(PageLinkGoogleMarkup,str[1],header,links,querystr)); //This works perfect
And when I tries to add this above code to StringBuilder instead of HtmlTextWriter it gives error:
As there is big loop so I decided to go with appending everything to StringBuilder first and at last rendering using writer.Write (Whole html in a bunch), please below code
StringBuilder sb = new StringBuilder();
sb.Append(string.format(PageLinkGoogleMarkup,str[1],header,links,querystr)); //here i get object reference error
writer.Write(sb.ToString());
Please suggest!!
Thanks.
One of the following variables is null when you are using it in the StringBuilder code (but was not null when you used it in the working code):
str (or the item in index [1])
header
links
querystr
Use the debugger to figure out which one is null, fix that, and the code should work fine.