0

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

Source Error:


Line 156:                                <img class="carouselImg" src="<%=slide.ImageUrl%>" />
Line 157:                                <%
Line 158:foreach (var el in slide.Hotspots.Elements())
Line 159:{ 
Line 160:      var Top = el.Element("top").Value;    








NullReferenceException: Object reference not set to an instance of an object.]
   ASP.views_home_index_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\_ 
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266
4

3 回答 3

1
if ((slide != null) && (slide.Hotspots != null))
{
    var elements = slide.Hotspots.Elements();
    if (elements != null)
    {
        foreach (var el in elements)
        {
            var element = el.Element("top");
            if (element.Value != null)
            {
                var Top = el.Element("top").Value;
                //...
            }
        }
    }
}

请注意,在上面的代码中,我假设 el.Element 是一种方法。如果还有其他问题,请提供其他信息。

于 2013-10-29T02:26:14.617 回答
0

你确认你有一个“顶级”元素吗?如果有些元素没有“顶部”,则应跳过 for 循环的这些迭代。

foreach (var el in slide.Hotspots.Elements()){
  var Top = el.Element("top")
  if( Top == null )
    continue;

   //Now you are free to access Top.Value;   
}
于 2013-10-29T00:01:40.840 回答
0

要么 要么slideHotspots空。与代码隐藏相比,调试它会很困难。

找到填充的代码slideHotspots验证它们是否产生了您期望的结果。

于 2013-10-29T00:02:45.047 回答