0

Can anybody suggest the cause of the following problem? My code is breaking on the line

integerList.Integers.Add(integer);

with the error message

Object reference not set to an instance of an object.

IntegerList.Integers is null which I suspect is the cause but what is the solution? Do I have to set IntegerList.Integers values when I initialize the variable so it is never null?

Models

public class IntegerList
{
    public int IntegerListID { get; set; }
    public string Direction { get; set; }
    public long Performance { get; set; }
    public virtual ICollection<Integer> Integers { get; set; }
}

public class Integer
{
    public int IntegerID { get; set; }
    public int IntegerValue { get; set; }
    public int IntegerListID { get; set; }
    public virtual IntegerList IntegerList { get; set; }
}

ViewModel

public class IntegerViewModel
{
    [UIHint("Integers")]
    public IntegerValueViewModel IntegerValues { get; set; }
    public string Direction { get; set; }
}

public class IntegerValueViewModel
{
    public ICollection<int> IntegerValue { get; set; }
}   

Controller

    [HttpPost]
    public ActionResult Index(IntegerViewModel integerViewModel)
    {
        if (ModelState.IsValid)
        {
            var integerList = new IntegerList
            {
                Direction = integerViewModel.Direction
            };

            foreach (var item in integerViewModel.IntegerValues.IntegerValue)
            {
                var integer = new Integer { IntegerValue = item };
                integerList.Integers.Add(integer);
            }
1

2 Answers 2

4

Because the Integers property is not initialized and it is null. You were trying to call the Add method on a null thing which gave you an error

Solution : Initialize the property in the IntegerList class constructor

public class IntegerList
{
    public int IntegerListID { get; set; }
    public string Direction { get; set; }
    public long Performance { get; set; }
    public virtual ICollection<Integer> Integers { get; set; }

    public IntegerList()
    {
      Integers =new List<Integer>();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Be careful of instantiating children objects as new List<Integer>. If you initialize the Integers child collection as a new List<Integer> in the constructor then when you want to do a .Where clause on the child property Integers you will get all the child Integers out of the Db for that parent record and then the .Where clause will be applied. If the child list is large then this affects performance dramatically.
0

When you do

var integerList = new IntegerList
            {
                Direction = integerViewModel.Direction
            };

the associated Integers is not initialized, you can initialize it in IntegerList contsctructor. or do the following

var integerList = new IntegerList
                {
                    Direction = integerViewModel.Direction,
                    Integers=new List<Integer>()

                };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.