Getting "Object Reference Error" while adding String.Format in StringBuilder

tia_kamakshi
tia_kamakshi used Ask the Experts™
on
Hi Experts,

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

Open in new window


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());

Open in new window


Please suggest!!

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011
Commented:
I suspect that the str array's length is less then 2, so str[1] is invalid.

Author

Commented:
@wdosanjos, no it is coming perfect that is what is told, above string is working fine when using with writer.write, and when same this is used with SB it gives error.

in the code (string.format(PageLinkGoogleMarkup,str[1],header,links,querystr))

there can be one condition if current page have got querystring then there will be value else it will blank.

can this be issue other wise everything is perfect as there value are coming perfect

Author

Commented:
There was null coming in stringbuilder object

Author

Commented:
There was coming null in stringbuilder object not in array

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial