1

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.

2
  • Do you mean "Object reference not set to an instance of an object"? Commented May 23, 2012 at 14:40
  • Yes, "Object reference not set to an instance of an object" Commented May 23, 2012 at 14:45

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't think that is the issue as it works perfect with writer.write..any ideas
You have not provided enough code to be entirely sure which one is null, but I'm fairly sure that this is the problem.
see onething "querystr" can be empty but not null as it if current page has querystring then only this variable will have value , does that can cause issue
Problem Resolved actually StringBuilder object was getting in NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.