繁体   English   中英

值不能为空。 参数名称:第一个

[英]Value cannot be null. Parameter name: first

当我运行下面的代码时,出现以下错误:

值不能为空。 参数名称:第一个

这是我的代码:

private async void CallWebApiDetails()
{
    WebApiService oWS = new WebApiService();
    lstLocalDBAlertsID = new List<string>();

    try
    {
        ErrorHandle err = await oWS.GetAllAlerts();
        var lstdistinct = err.lstServerAlertsIDs.Except(lstLocalDBAlertsID).ToList();

        if (lstdistinct != null)
        {
            var lstOrderedLst = lstdistinct.OrderBy(i => i).ToList();

            if (lstOrderedLst.Count > 0)
            {
                for (int i = 0; i < lstOrderedLst.Count; i++)
                {
                    AlertData oAlertData = new AlertData();
                    oAlertData.Where.AlertId.Value = lstOrderedLst[i];

                    if (!oAlertData.Query.Load())
                    {
                        ErrorHandle oErr = new ErrorHandle();
                        oErr = await oWS.getSpecificAlert(lstOrderedLst[i]);
                        await SaveAlertData(Convert.ToInt32(lstOrderedLst[i]), oErr);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        LogError oLE = new LogError();
        oLE.logEx(ex, "CallWebApiDetails");
    }
}

有人可以告诉我我的代码有什么问题吗?

Except扩展方法first具有this参数; 它被定义为

public static IEnumerable<TSource> Except<TSource>(
    this IEnumerable<TSource> first, IEnumerable<TSource> second)
{
    if (first == null)
    {
        throw Error.ArgumentNull("first");
    }
    // ...
}

所以大概在这一行:

var lstdistinct = err.lstServerAlertsIDs.Except(lstLocalDBAlertsID).ToList()

err.lstServerAlertsIDs值为null 所以:解决这个问题。

注意:最好将调试器与断点一起使用,或者至少要使用堆栈跟踪来确定哪一行失败。 您不能总是(甚至经常)推断这样的上下文。

暂无
暂无

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

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