Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何调试"';系统异常';发生在System.Core.dll中,但未在用户代码中处理;_C#_Argumentnullexception - Fatal编程技术网

C# 如何调试"';系统异常';发生在System.Core.dll中,但未在用户代码中处理;

C# 如何调试"';系统异常';发生在System.Core.dll中,但未在用户代码中处理;,c#,argumentnullexception,C#,Argumentnullexception,我是C#新手,对此有点困惑,主要是因为在过去的几个月里,这个web应用程序的一切都运行得很好,而这个问题才刚刚开始,似乎几个月来代码没有做任何更改。我在这里读过几篇关于这个错误的帖子,但所有的答案都有点笼统,不符合我的知识水平 引发的错误是: System.Core.dll中发生“System.ArgumentNullException”类型的异常,但未在用户代码中处理 其他信息:值不能为空 代码是: [Route("ajax/addOrUpdateSource")] [HttpPost] pu

我是C#新手,对此有点困惑,主要是因为在过去的几个月里,这个web应用程序的一切都运行得很好,而这个问题才刚刚开始,似乎几个月来代码没有做任何更改。我在这里读过几篇关于这个错误的帖子,但所有的答案都有点笼统,不符合我的知识水平

引发的错误是:

System.Core.dll中发生“System.ArgumentNullException”类型的异常,但未在用户代码中处理

其他信息:值不能为空

代码是:

[Route("ajax/addOrUpdateSource")]
[HttpPost]
public JsonResult AddOrUpdateSource(Source source, string html)
{
    var htmlDoc = new HtmlAgilityPack.HtmlDocument();
    htmlDoc.LoadHtml(html);

    var response = new Dictionary<string, object>();
    var labels = htmlDoc.DocumentNode.SelectNodes("//label");
    List<string> categories = new List<string>();
    string[] vals = new string[] { };

     for(int n = 1; n < labels.Count(); n++)
    {
        var label = labels[n].Attributes[0].Value;

        if (label.Contains( "btn") && label.Contains("btn-primary"))
            categories.Add(labels[n].InnerText.Trim());
    }

    response.Add("source", null);

    try
    {
        string catValues = String.Join(", ", categories.ToArray());

        source.LastUpdateDateTime = DateTimeOffset.UtcNow;
        source.LastUpdateUser = User.Identity.Name;
        source.ProductCategory = catValues;

        // save the source
        var updatedSource = _taskService.AddOrUpdateSource(source);

        // prepare the response
        response["source"] = updatedSource;
    }
    catch(Exception exc)
    {
        response.Add("error", exc.Message);
    }

    return Json(response);

代码可能没有更改,但加载的html文档可能不包含任何与“//label”匹配的节点


因此标签为null,调用Count将抛出

它告诉您标签为null。似乎HTML中没有正确的节点。有人改了吗?无论如何,在SelectNodes调用之后,您应该测试null。这似乎是一条非常明确的错误消息,
SelectNodes(“//label”)
上没有匹配项,因此您不能调用
Count()
。除了其他人所说的之外,您可能还需要在继续之前检查实例是否存在,并正常失败。由于
SelectNodes()
可能返回null,因此谨慎的做法是使用
if(labels==null)//优雅地失败来检查是否返回null如果您使用的是较新版本的.NET,则可以使用
labels?.Count()
它将为您执行检查,并在标签为空时返回not execute循环。将代码行更改为
var labels=htmlDoc.DocumentNode.SelectNodes(“//label”)??Enumerable.Empty()这将确保它返回一个空的可枚举项,而不是null。这就是您的问题。:)您需要了解为什么要将
Word
发送到该方法。谢谢Erix。虽然你的答案对我来说绝对是最有意义的,但是html文档中绝对没有标签节点。你可能会发现它的用处@Stpete111@mjwills谢谢你的链接,这几乎正是我所经历的。事实上,按照Alex的建议更改代码确实使错误停止抛出。不幸的是,在执行AddorUpdateSource时,我没有完成我应该完成的任务。所以我真正需要弄清楚的是,为什么它看到标签节点为NULL。@Stpete111:大小写正确吗?换句话说,它的拼写是
label
,而不是html中的
label
label
吗?@NotMe-据我所知,是的。我正在查找与此代码相关的cshtml文档。我想那是对的。
$scope.performSaveSource = function () {
        GlobalService.togglePleaseWait(true);

        $scope.source.SourceStatusId = $scope.source.SelectedSourceStatus.SourceStatusId;
        $scope.source.SourceStatus = $scope.source.SelectedSourceStatus.SourceStatusId;
        $scope.source.DataTypeId = $scope.source.SelectedDataType.DataTypeId;

        // pass the source and activity to be saved
        $http({
            url: "/ajax/addorupdatesource",
            method: "POST",
            data: {
                source: $scope.source,
                html: $('html').context.all["143"].innerHTML
            }
        })
        .then(function (response) {
            GlobalService.togglePleaseWait(false);

            if (response.status == 200) {
                if (response.data.error != null) {
                    alert(response.data.error);
                    return;
                }

                // update our local copies
                $scope.setSource(response.data.source);
            }
        });
    };