Avatar of tia_kamakshi
Flag 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


Open in new window

Please suggest!!


Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

@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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck