-1

i have a problem, i created an object in C# like this:

public class logis
        {
            public string codigo { get; set; }
            public List<decimal> Necesidades { get; set; }
            decimal SumaNecesidades{get;set;}
        }

then i do something like this:

logisva logi = new logis();
logi.codigo = oDataReader.GetValue(0).ToString();
logi.Necesidades.Add(0);

But when i execute my code i get a null reference exception error. Object reference not set to an instance of an object. on the last line logi.Necesidades.Add(0);

Any idea why i get this error?

Ion
  • 549
  • 1
  • 11
  • 25
  • `logi.Necesidades` is null. You need to initialize it somewhere - either in the getter or in a constructor. – D Stanley Feb 15 '16 at 17:24
  • Or with an auto-property initializer. C# 6 has been around for a while, and IMO unless there's a specific target version, we should start using its features already :-) – Jcl Feb 15 '16 at 17:28

1 Answers1

2

In C# the properties do not initialize/create the List<ofType> object automatically. You need to create the list explicitely:

public class logis
{
    public string codigo { get; set; }
    public List<decimal> Necesidades { get; set; }
    decimal SumaNecesidades{get;set;}

    public logis() 
    { 
        this.Necesidades = new List<decimal>(); 
    }
}

Another option is to create the list in the getter resp. setter (so to say your own lazy initialization, downside - introduces more code, advantage no need to override every contructor):

public class logis
{
    public string codigo { get; set; }
    decimal SumaNecesidades{get;set;}

    private List<decimal> necesidades = null;
    private void initNecesidades() 
    {
        if (this.necesidades == null) 
        { 
            this.necesidades = new List<decimal>(); 
        }
    }
    public List<decimal> Necesidades 
    { 
        get
        {
            this.initNecesidades();
            return this.necesidades;
        }
        set
        {
            this.initNecesidades();
            this.necesidades = value;
        }
    }
}

Yet another option would be to use the new C# 6.0 features (if it is an option to use/already using the latest .NET Framework version) as already suggested in the comments by @Jcl:

public List<decimal> Necesidades { get; set; } = new List<decimal>()
keenthinker
  • 7,645
  • 2
  • 35
  • 45
  • Ohh, yes you are right i am not thinkinking on this, Thanks – Ion Feb 15 '16 at 17:27
  • 1
    If using C# 6, I'd use: `public List Necesidades { get; set; } = new List()` and wouldn't pollute my constructor :-) – Jcl Feb 15 '16 at 17:29
  • Note (about latest edit) that most C# 6 features don't need the latest framework versions, they just need a recent compiler. I haven't tested it, but I'm sure autoproperty initializers would work on .NET 3.5... possibly 2.0 too – Jcl Feb 15 '16 at 21:30
  • 1
    Also, your "lazy" initializer could be rewritten (without the need for C#6.0 even) as `get { return (necesidades ?? necesidades = new List()); } set { necesidades = value; }`, no need for the extra method and it's not a lot of code either. – Jcl Feb 15 '16 at 21:36