Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tia_kamakshi

ASKER

@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
There was null coming in stringbuilder object
There was coming null in stringbuilder object not in array