1

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?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • what about a debugger? have you tried to debug the exception – Jehof Feb 28 '14 at 11:58
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jehof Feb 28 '14 at 11:59
  • By the way, why is that property `virtual`? – Jon Feb 28 '14 at 12:00

6 Answers6

2

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

public class Vuln : VulnSmall
{
    public Vuln()
    {
       VulnerabilityReferences = new List<VulnerabilityReference>();
    } 
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
2

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.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2

In constructor of the Vulnclass you need to do this:

public Vuln()
{
   VulnerabilityReferences  = new List<VulnerabilityReference>();
}
Only a Curious Mind
  • 2,807
  • 23
  • 39
2

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>();
    }
}
Jon
  • 428,835
  • 81
  • 738
  • 806
2

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>();
  }

}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

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; 
      }
    }
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215