AndreaNobili
AndreaNobili

Reputation: 42957

Why I can't add this object into this list?

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?

Upvotes: 1

Views: 119

Answers (6)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You are not initializing VulnerabilityReferences list. Thus it has null value and it throws exception when you try to call its Add method. You can create new list in constructor of Vuln class:

public class Vuln : VulnSmall
{
    public Vuln()
    {
        VulnerabilityReferences = new List<VulnerabilityReference>();
    }

    // ...
}

From C# Specification 10.4.4 Field initialization (with auto-implemented property you still have field, but its generated by compiler for you):

The initial value of a field, whether it be a static field or an instance field, is the default value (Section 5.2) of the field's type.

List is a reference type, so by default it will have value null.

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You should create a List<VulnerabilityReference> to insert item into:

...

currentNVDVuln.VulnerabilityReferences = new List<VulnerabilityReference> VulnerabilityReferences();

currentNVDVuln.VulnerabilityReferences.Add(currentVulnRef);

But a better solution is to redesign VulnSmall

  public class Vuln : VulnSmall {
    // List is created on Vuln creation
    private List<VulnerabilityReference> m_VulnerabilityReferences =
      new List<VulnerabilityReference>();

    // You're not going to set a whole list, are you?
    // That's why no "set" here
    public IList<VulnerabilityReference> VulnerabilityReferences {
      get {
        return m_VulnerabilityReferences; 
      }
    }
  }

Upvotes: 1

Guffa
Guffa

Reputation: 700192

You get a null reference exception because the reference to the list is null. You only have a reference for a list, you have to create the actual list at some point:

VulnerabilityReferences = new List<VulnerabilityReference>();

You can do that for example in the constructor of the class:

public class Vuln : VulnSmall {

  public virtual List<VulnerabilityReference> VulnerabilityReferences { get; set; }    

  public Vuln(){
    VulnerabilityReferences = new List<VulnerabilityReference>();
  }

}

Upvotes: 2

Jon
Jon

Reputation: 437336

You are trying to add something to a list that does not yet exist.

It isn't enough to define your list property, you must also initialize it. For example:

public class Vuln : VulnSmall
{
    public virtual List<VulnerabilityReference> VulnerabilityReferences { get; set; } 

    public Vuln()
    {
        this.VulnerabilityReferences = new List<VulnerabilityReference>();
    }
}

Upvotes: 2

Only a Curious Mind
Only a Curious Mind

Reputation: 2857

In constructor of the Vulnclass you need to do this:

public Vuln()
{
   VulnerabilityReferences  = new List<VulnerabilityReference>();
}

Upvotes: 2

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

You should initialize VulnerabilityReferences , it would better if you do it in your constructor:

public class Vuln : VulnSmall
{
    public Vuln()
    {
       VulnerabilityReferences = new List<VulnerabilityReference>();
    } 
}

Upvotes: 2

Related Questions