I'm new to C# and a bit baffled by this, mainly because everything has worked perfectly with this web app for the last several months, and this issue just started, seemingly with no changes having been made to the code in months. I've read several posts here related to this error, but all of the answers are a little general for my knowledge-level.
The error that is thrown is:
An exception of type 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code
Additional information: Value cannot be null.
The code is:
[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);
See the screenshot below to see exactly where Visual Studio attaches the error to during debug. (the line containing "n < label.Count()")
How can I fix this? I don't expect I'll be able to figure out why everything was ok, and suddenly this started happening, but if I can fix it, I'll be able to move forward.
Updates
EDIT 1 - after all of the very helpful input below, it has become clear that what I truly need to figure out is why the app is suddenly reporting NULL for the label nodes. I am able to prevent the error from being thrown now, but before this phantom change took place, the label nodes were never NULL, and the execution accomplished what it was designed to.
EDIT 2 - per guidance from @mjwills here is the code for the function that sends the data:
$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);
}
});
};