0

I have a nested collection in side an object:

public class Question
{   
    public AnswerObjectCollection Answers
    {
        get;
        private set;
    }
}

When I try to add answers to the AnswerObjectCollection in the Question object I get following exception:

Object reference not set to an instance of an object

Question currQuestion = new Question();
currQuestion.Answers.AddRange(GetAnswersByQuestion(currQuestion.QuestionIdentity));

If I try to create the answer object first (which does work) I can't add that either

AnswerObjectCollection answer = new AnswerObjectCollection();
answer.AddRange(GetAnswersByQuestion(currQuestion.QuestionIdentity));
currQuestion.Answers.AddRange(answer);

If I try mapping the objects I don't get an error but the currQuestion.Answers variable is null

Mapper.CreateMap(typeof(AnswerObjectCollection), typeof(AnswerObjectCollection));
Mapper.CreateMap(typeof(Answer), typeof(Answer));
Mapper.Map(answer, currQuestion.Answers);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
robbins
  • 25
  • 5
  • Please mark an answer as solved, if one helped you solving your problem. – cramopy May 12 '15 at 16:22
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 12 '15 at 16:36

3 Answers3

0

You need to add a constructor to your Question class

public class Question {
    public Question() {
       Answers = new AnswerObjectCollection();
    }
    public AnswerObjectCollection Answers {
        get;
        private set;
    }
}

This will instantiate your Answers property.

Ulric
  • 826
  • 5
  • 16
  • Note that a constructor is not always invoked, for example when deserializing an object using some of the framework's serializers. – Eric J. May 12 '15 at 15:56
  • @EricJ. I think if you specify a parameter-less constructor, it will be called when instantiating an instance. I'll have to verify tho ;) – rae1 May 12 '15 at 16:02
  • @EricJ: I have deserialised a lot and I haven't encountered a problem before. But maybe I was just 'lucky' :) . I'll go do some research on this. Thanks for pointing out a possible problem. – Ulric May 12 '15 at 16:07
  • Here's the problem http://stackoverflow.com/q/9419743/141172 – Eric J. May 12 '15 at 16:14
  • @EricJ: I tested the code using JSON de/serialising and the constructor is triggered. I do not know if it works for all de/serialisers though. From your link, it looks like it might simply depend on the de/serialiser that is being used. – Ulric May 12 '15 at 16:15
  • Some but not all serializers opt not to fully construct the object. Not sure about the JSON serializer. – Eric J. May 12 '15 at 16:20
  • @robbins: NP:) I recommend testing it when de/serialising, to ensure the constructor is being hit. – Ulric May 13 '15 at 14:29
0

Easy as this:

After creating a Question by calling

Question currQuestion = new Question();

you have to create an instance of the AnswerObjectCollection

currQuestion.Answers = new AnswerObjectCollection();

and it will work.

Or add this to your code:

public Question()
{
    Answers = new AnswerObjectCollection();
}
cramopy
  • 3,459
  • 6
  • 28
  • 42
  • Note that a constructor is not always invoked, for example when deserializing an object using some of the framework's serializers. – Eric J. May 12 '15 at 15:57
  • @EricJ. that#s true, but I think this might be ok, if he invokes it before. – cramopy May 12 '15 at 15:58
  • It's probably fine for this application, but it's a surprising and *potentially* important fact so I wanted to point it out. – Eric J. May 12 '15 at 15:58
  • @EricJ. you're right (and allowed, what's the purpose of `SO`) to do so, please go on this way! New c# users might be grateful for your comment, when having this problem! – cramopy May 12 '15 at 16:01
  • @cramopy - thanks for you quick, and clear response, I used your second suggestion. – robbins May 13 '15 at 14:29
  • @robbins thanks a lot, if if helped you please tick my answer as accepted :D thanks again – cramopy May 13 '15 at 14:38
0

The property Answers is not being initialized. You need to do this when constructing the class Question,

public Question()
{
    Answers = new AnswerObjectCollection();
}

By default, all properties and field will be initialized to a default value (default(T)). For reference properties (and fields), the default is null, thus why you ran into a NullReferenceException.

A similar problem for,

AnswerObjectCollection answer = new AnswerObjectCollection();
answer.AddRange(GetAnswersByQuestion(currQuestion.QuestionIdentity));

here you are just creating a separate variable, not related to the property Answers of class Question. Thus, when you do this,

currQuestion.Answers.AddRange(answer);

you run into the same issue as before.

Both can be fixed by initialization the property in the constructor.

rae1
  • 6,066
  • 4
  • 27
  • 48