简体   繁体   English

为什么我会收到此元素为空的错误?

[英]Why am I getting this element null error?

I'm getting this error, not really sure why.我收到此错误,不确定为什么。 Please help, this is urgent.请帮忙,这是紧急的。

UPDATE更新

if (flights.Count() >= 1)
        {
            int count = flights.Count();
            lblNumResults.Text = count.ToString();
            gvAvailableFlights.DataSource = flights;
            gvAvailableFlights.DataBind();
        }

Server Error in '/WebSite3' Application. “/WebSite3”应用程序中的服务器错误。 Value cannot be null.值不能为空。 Parameter name: element Description: An unhandled exception occurred during the execution of the current web request.参数名称:element 说明:在执行当前 Web 请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code.请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

Exception Details: System.ArgumentNullException: Value cannot be null.异常详细信息:System.ArgumentNullException:值不能为空。 Parameter name: element参数名称:元素

Source Error:源错误:

The source code that generated this unhandled exception can only be shown when compiled in debug mode.生成此未处理异常的源代码只能在调试模式下编译时显示。 To enable this, please follow one of the below steps, then request the URL:要启用此功能,请按照以下步骤之一操作,然后请求 URL:

  1. Add a "Debug=true" directive at the top of the file that generated the error.在生成错误的文件顶部添加“Debug=true”指令。 Example:例子:

    <%@ Page Language="C#" Debug="true" %> <%@ 页面语言="C#" Debug="true" %>

or:或者:

2) Add the following section to the configuration file of your application: 2) 将以下部分添加到您的应用程序的配置文件中:

Note that this second technique will cause all files within a given application to be compiled in debug mode.请注意,第二种技术将导致在调试模式下编译给定应用程序中的所有文件。 The first technique will cause only that particular file to be compiled in debug mode.第一种技术将导致仅在调试模式下编译该特定文件。

Important: Running applications in debug mode does incur a memory/performance overhead.重要提示:在调试模式下运行应用程序确实会产生内存/性能开销。 You should make sure that an application has debugging disabled before deploying into production scenario.在部署到生产场景之前,您应该确保应用程序已禁用调试。

Stack Trace:堆栈跟踪:

[ArgumentNullException: Value cannot be null. [ArgumentNullException:值不能为空。 Parameter name: element]参数名称:元素]
System.Xml.Linq.XElement.op_Explicit(XElement element) +116474 System.Xml.Linq.XElement.op_Explicit(XElement 元素) +116474
searchresult.b__1d(XElement f) +64 searchresult.b__1d(XElement f) +64
System.Linq.WhereSelectEnumerableIterator 2.MoveNext() +151 System.Linq.Enumerable.Count(IEnumerable 1 source) +201 System.Linq.WhereSelectEnumerableIterator 2.MoveNext() +151 System.Linq.Enumerable.Count(IEnumerable 1 源) +201
searchresult.FillAvailableFlightsGridView() +721 searchresult.Page_Load(Object sender, EventArgs e) +37 searchresult.FillAvailableFlightsGridView() +721 searchresult.Page_Load(Object sender, EventArgs e) +37
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91 System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Version Information: Microsoft .NET Framework Version:4.0.30319;版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET Version:4.0.30319.1 ASP.NET 版本:4.0.30319.1

In your code you have function called FillAvailableFlightsGridView and in that function you call Count() of some collection which is null.在您的代码中,您有一个名为FillAvailableFlightsGridView函数,在该函数中,您调用了某个集合的Count() ,该集合为 null。

Before calling the Count() check if it's null and in such case, assume zero length or do whatever handling you want.在调用Count()之前检查它是否为空,在这种情况下,假设长度为零或做任何你想做的处理。

For example:例如:

int myCount = 0;
if (myCollection != null)
   myCount = myCollection.Count();

Edit: optimized code in your case would be:编辑:在您的情况下优化的代码将是:

int flightsCount = (flights == null) ? 0 : flights.Count();
if (flightsCount >= 1)
{
    lblNumResults.Text = flightsCount.ToString();
    gvAvailableFlights.DataSource = flights;
    gvAvailableFlights.DataBind();
}

No need to call Count() twice, as you saw it contains internal code that might be heavy.无需调用Count()两次,因为您看到它包含可能很重的内部代码。

Change this:改变这个:

if (flights.Count() >= 1)
        {
            int count = flights.Count();
            lblNumResults.Text = count.ToString();
            gvAvailableFlights.DataSource = flights;
            gvAvailableFlights.DataBind();
        }

To this:对此:

 if (flights != null && flights.Count() >= 1)
    {
        lblNumResults.Text = (string)flights.Count();
        gvAvailableFlights.DataSource = flights;
        gvAvailableFlights.DataBind();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM