-1

I have a class that looks something like this:

public class Parent
{
    private string sFirst_name1;
    public string sParent1FirstName
    {
        get { return sFirst_name1; }
        set { sFirst_name1 = value; }
    }

    private string sLast_name1;
    public string sParent1LastName
    {
        get { return sLast_name1; }
        set { sLast_name1 = value; }
    }

    private List<int> lChild_ID;
    public List<int> lChildID
    {
        get { return lChild_ID; }
        set { lChild_ID = value; }
    }
public Parent(string sP1FN, string sP1LN, List<int> lCID)
    {
        lChild_ID= new List<int>();
        sFirst_name1 = sP1FN;
        sLast_name1 = sP1LN;
        lChild_ID = lCID;

    }

I create an instance of the class and add it to a List of type Parent with the line:

lParents.Add(new Parent(sParent1FNTemp, sParent1LNTemp,lParentChildIDTemp));

Where lParentChildIDTemp is just a null integer List.

Later on, I am trying to update the int list of the object at a specified index by saying:

lParents[iIndex].lChildID.Add(++iIDCounters);

And I get a null reference exception. I'm not too sure why, but I think it has something to do with my constructor for my class. The weird thing is, I have another class that does pretty much the same thing but it doesn't throw the exception. If anyone has any clues, I would be greatful.

Evan M.
  • 1
  • 1
  • 2
    `Where lParentChildIDTemp is just a null integer List.` ...... `And I get a null reference exception`. I think you answered your own question. – Sam Axe Jun 02 '17 at 22:19
  • 1
    You are assigning `lChild_ID` twice in the constructor. – dotNET Jun 02 '17 at 22:20
  • it would be helpful if you noted where the exception is being thrown from - there are a few places it looks like you could have a null exception under normal execution...it seems likely that it is a null being passed into your constructor – Robert Petz Jun 02 '17 at 22:37
  • See marked duplicate for general advice debugging and fixing `NullReferenceException`. If that is insufficient, post a new question in which you include a good [mcve] that reliably reproduces the problem. – Peter Duniho Jun 02 '17 at 23:51

3 Answers3

0

Drop the List of int argument in the ctor and why not use auto props:

public class Parent
{
    public Parent(string sP1FN, string sP1LN)
    {
        LChildID = new List<int>();
        sFirst_name1 = sP1FN;
        sLast_name1 = sP1LN;
    }

    public List<int> LChildID { get; set; }

If you're not familiar with auto-implemented properties, see https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

Plus, you could also overload the ctor:

    public Parent(string sP1FN, string sP1LN, List<int> lCID)
    {
        LChildID = lCID;
        sFirst_name1 = sP1FN;
        sLast_name1 = sP1LN;
    }
Elmer Ortega
  • 469
  • 4
  • 12
  • there are so many things about his code style that are very unusual for C# - but I'm assuming he is not using auto props because this is pseudocode for this question – Robert Petz Jun 02 '17 at 22:38
  • also - this is not an answer to his question and is quite unrelated to the question at hand...in the future, use comments for this stuff – Robert Petz Jun 02 '17 at 22:40
  • 1
    @RobertPetz I see what you mean, edited it. – Elmer Ortega Jun 02 '17 at 22:41
0

Give your property is null, you should make sure you are not access null values to call methods or properties. If you do that, you will get an NullReferenceException. Try this:

var childIds = lParents[iIndex].lChildID;
if (childIds != null)
{
    childIds.Add(++iIDCounters);
}

In your constructor, you could check the if the argument is null and define a default list int. If you for to set the argument, even if it is null, you will get null.

public Parent(string sP1FN, string sP1LN, List<int> lCID)
{
    sFirst_name1 = sP1FN;
    sLast_name1 = sP1LN;
    if (lCID == null)       
    {
       lChild_ID = new List<int>();
    }
    else
    {
       lChild_ID = lCID;
    }
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0

Since apparently you want to be able to pass in the lChildID during construction, change the constructor as so:

public Parent(string sP1FN, string sP1LN, List<int> lCID) {
    sFirst_name1 = sP1FN;
    sLast_name1 = sP1LN;
    lChild_ID = lCID ?? new List<int>();
}
NetMage
  • 26,163
  • 3
  • 34
  • 55