-1

I have the following situation. In a method I have the following code:

XmlNodeList n_vulnerablesystemslist = n_alertdocument.SelectNodes(
    "./x:VulnerableSystems/x:VulnerableSystem", nsmgr);

foreach (XmlNode n_vulnerablesystem in n_vulnerablesystemslist)
{
    DataModel.Vulnerability.VulnerableSystem currentVulnerableSystem = 
        new DataModel.Vulnerability.VulnerableSystem();

    currentVulnerableSystem.Title = 
        n_vulnerablesystem.SelectSingleNode("./x:Title", nsmgr) == null 
        ? "" 
        : n_vulnerablesystem.SelectSingleNode("./x:Title", nsmgr).InnerText;
    currentDeepSightVuln.VulnerabilityVulnerableSystems.Add(
        currentVulnerableSystem);
}

Where VulnerabilityVulnerableSystems is the following collection definied inside the Vuln currentDeepSightVuln object:

public virtual List<VulnerableSystem> VulnerabilityVulnerableSystems
    { get; set; }

The problem is that when try to execute the Add operation the following exception is thrown:

  • ex {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}

This seems to me very strange because, in the debugger I can see that the VulnerableSystem currentVulnerableSystem object is initialized and that its Title is correctly valorized.

So what is the problem? What am I missing? How can I solve it?

Tnx

BenG
  • 13,680
  • 4
  • 41
  • 57
AndreaNobili
  • 34,200
  • 85
  • 240
  • 456

1 Answers1

4

Because the List VulnerabilityVulnerableSystems has not been initialized. So the Add method operates on a Null reference.

Try VulnerabilityVulnerableSystems = new List<VulnerableSystem>();

quantdev
  • 22,595
  • 5
  • 47
  • 84