字符串初始化NullReferenceException

时间:2016-06-01 10:28:33

标签: c# string nullreferenceexception

我正在初始化一个字符串并且它正在抛出一个NullReferenceException:

string HTML = 
            "<div class=\"table-responsive formMOE\">" +
            "<table class=\"table\">" +
            "<thead class=\"white\"><tr><th>ID</th><th>Name</th><th>Type</th><th>Description</th><th>Ingredients</th><th>Price</th></tr></thead>" +
            "<tbody data-link=\"row\" class=\"rowlink\">" +
            "<tr>" +
            "<td><a href=\"Products.aspx?ProductID=" + product.ProductID + "\"></a>" + product.ProductID +
            "</td><td>" + product.Name +
            "</td><td>" + product.Type +
            "</td><td>" + product.Description +
            "</td><td>" + product.Ingredients +
            "</td><td>" + product.Price +
            " <span class=\"fa fa-shekel\"></span></td><td>" +
            "</td>" +
            "</tr>" +
            "</tbody></table></div>";

即使我使用此代码:

string HTML =
        "<div class=\"table-responsive formMOE\">" +
        "<table class=\"table\">" +
        "<thead class=\"white\"><tr><th>Name</th><th>Description</th><th>Price</th></tr></thead>" +
        "<tbody data-link=\"row\" class=\"rowlink\">";

在另一部分中,没有得到NullReferenceException。

我尝试将字符串设置为随机值,例如&#34; hi&#34;然后将其设置为我需要的实际值,没有用。

1 个答案:

答案 0 :(得分:2)

我能看到的唯一问题是你的产品对象是空的。

第二个

string HTML =
"<div class=\"table-responsive formMOE\">" +
"<table class=\"table\">" +
"<thead class=\"white\"><tr><th>Name</th><th>Description</th><th>Price</th></tr></thead>" +
"<tbody data-link=\"row\" class=\"rowlink\">";

无法生成空例外。

相关问题