如何将对象集合添加到对象变量集合中? [重复]

时间:2022-12-06 07:31:08

This question already has an answer here:

这个问题在这里已有答案:

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:

当我尝试在Question对象中添加AnswerObjectCollection的答案时,我得到以下异常:

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

如果我尝试映射对象,我没有得到错误,但currQuestion.Answers变量为null

Mapper.CreateMap(typeof(AnswerObjectCollection), typeof(AnswerObjectCollection));
Mapper.CreateMap(typeof(Answer), typeof(Answer));
Mapper.Map(answer, currQuestion.Answers);

3 个解决方案

#1


You need to add a constructor to your Question class

您需要在Question类中添加构造函数

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

This will instantiate your Answers property.

这将实例化您的Answers属性。

#2


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

Answers属性未初始化。在构造类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.

默认情况下,所有属性和字段都将初始化为默认值(默认值(T))。对于引用属性(和字段),默认值为null,因此您遇到了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,

在这里,您只是创建一个单独的变量,与类问题的Answers属性无关。因此,当你这样做时,

currQuestion.Answers.AddRange(answer);

you run into the same issue as before.

你遇到了和以前一样的问题。

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

两者都可以通过在构造函数中初始化属性来修复。

#3


Easy as this:

这很容易:

After creating a Question by calling

通过调用创建问题后

Question currQuestion = new Question();

you have to create an instance of the AnswerObjectCollection

你必须创建一个AnswerObjectCollection的实例

currQuestion.Answers = new AnswerObjectCollection();

and it will work.

它会起作用。

Or add this to your code:

或者将其添加到您的代码中:

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

#1


You need to add a constructor to your Question class

您需要在Question类中添加构造函数

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

This will instantiate your Answers property.

这将实例化您的Answers属性。

#2


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

Answers属性未初始化。在构造类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.

默认情况下,所有属性和字段都将初始化为默认值(默认值(T))。对于引用属性(和字段),默认值为null,因此您遇到了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,

在这里,您只是创建一个单独的变量,与类问题的Answers属性无关。因此,当你这样做时,

currQuestion.Answers.AddRange(answer);

you run into the same issue as before.

你遇到了和以前一样的问题。

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

两者都可以通过在构造函数中初始化属性来修复。

#3


Easy as this:

这很容易:

After creating a Question by calling

通过调用创建问题后

Question currQuestion = new Question();

you have to create an instance of the AnswerObjectCollection

你必须创建一个AnswerObjectCollection的实例

currQuestion.Answers = new AnswerObjectCollection();

and it will work.

它会起作用。

Or add this to your code:

或者将其添加到您的代码中:

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