I am absolutly new in C# and I have the following problem trying to insert an object into a Collection.
So I have a base class named VulnSmall that contains some properties, then I have a class named Vuln that extends the previous VulnSmall class adding to it some properties including a list named VulnerabilityReferences, as you can see in the following code snippet:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataModel.Vulnerability
{
public class Vuln : VulnSmall
{
.......................
.......................
.......................
public virtual List<VulnerabilityReference> VulnerabilityReferences { get; set; }
.......................
.......................
.......................
}
}
Ok,
in another class I have something like it:
DataModel.Vulnerability.Vuln currentNVDVuln = new DataModel.Vulnerability.Vuln();
// Creation of the VulnerabilityReference object and initialization of its fields:
DataModel.Vulnerability.VulnerabilityReference currentVulnRef = new DataModel.Vulnerability.VulnerabilityReference();
currentVulnRef.Title = "My Title";
currentVulnRef.Description = "My Descripion"
currentVulnRef.URL = "www.myurl.com"
// Adding the previous obkect to the VulnerabilityReferences list field:
currentNVDVuln.VulnerabilityReferences.Add(currentVulnRef);
As you can see I have a Vuln object named currentNVDVuln (that contains the VulnerabilityReferences list as its field), I create a VulnerabilityReference object (named currentVulnRef) and I try to add it to this list.
But don't work and when try to execute this line:
currentNVDVuln.VulnerabilityReferences.Add(currentVulnRef);
it go into error and throw this Exception:
{"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}
Why? What could be the problem? What am I missing?