0

I try to display the table using StringBuilder but it does not work. It causes an error say "Object reference not set to an instance of an object" at sb.Append(string.Format(. Please help. Thank you.

            foreach (Item item in list)
            {

                sb.Append(string.Format(
                    @"<table >
                        <tr>
                            <th rowspan ='6' width='150px'><img runat='server' src='{3}'/>
                            </th>
                            <th width='50px'>Title: </th>
                            <td>{0}</td>
                        </tr>
                        <tr>
                            <th>Price:</th>
                            <td>{1}</td>
                        </tr>
                        <tr>
                            <th>Available: </th>
                            <td>{2}</td>                                
                        </tr>
                    </table>",  item.ItemAttributes.Publisher,item.ItemAttributes.ListPrice.ToString(), item.ItemAttributes.NumberOfItems, item.SmallImage.URL));
                lblItemResponse.Text = sb.ToString();


            }
user1874435
  • 161
  • 13
  • With just the code, it will not be possible. You need to put a debugger and see which object of item there is null value. – Prash Jul 26 '13 at 02:59
  • 1
    Try to put a breakpoint in this line lblItemResponse.Text = sb.ToString(); and hover all the objects to find out which item is null. – jomsk1e Jul 26 '13 at 03:02
  • 1
    BTW, call `AppendFormat()` – SLaks Jul 26 '13 at 03:15
  • I just don't know what happened but when I add try/catch in, every thing works well. – user1874435 Jul 26 '13 at 03:31
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 26 '13 at 05:35

0 Answers0