0

我正在使用 MVC 3,并且正在使用数据阅读器来创建具有子项的项目列表。当我添加子项时,我得到“对象引用未设置为对象的实例”。使用以下代码:

QuestionaireLine question = new QuestionaireLine();
         question.Question_ID = Convert.ToInt32(reader["Question_ID"]);
         question.Question_Answer = reader["Question_Answer"].ToString();
         ...etc..
         currentGroup.Lines.Add(question); //exception thrown here

型号:

 public class Questionaire
    {
        public int Question_Group_Id { get; set; }
        public string Question_Group_Name { get; set; }
        public int Question_Group_Indent { get; set; }
        public int Question_Group_Order { get; set; }
        public List<QuestionaireLine> Lines { get; set; }
    }



public class QuestionaireLine
    {
         public int Question_ID { get; set; }
         public string Question_Label { get; set; }
         public string Question_Answer { get; set; }
         public int Data_Type { get; set; }
         public int Control_Type { get; set; }
         public string Data_Choices { get; set; }
         public int Data_Max_Length { get; set; }
         public bool Issue_Tagged { get; set; }
         public int Question_Order { get; set; }
         public string NumberedQuestion
         {
             get { return String.Format("{0}. {1}", Question_Order, Question_Label); }
         }
    }

整个代码: //我错过了什么?

using (var conn = new SqlConnection(_connectionString))
            {
                List<Questionaire> groups = new List<Questionaire>();
                var com = new SqlCommand();
                com.Connection = conn;
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Add(new SqlParameter
                {
                    ParameterName = "@Review_ID",
                    Value = reviewID
                });
                com.CommandText = "Review_Get_Question_Groups_Answers";

                conn.Open();

                // Get the reader
                SqlDataReader reader = com.ExecuteReader();

                // Process each result in the result set
                int currQuestionGroupId = 0;

                Questionaire currentGroup = null;
                while (reader.Read())
                {
                    var questionGroupId = Convert.ToInt32(reader["Question_Group_Id"]);
                    if (questionGroupId != currQuestionGroupId)
                    {
                        currQuestionGroupId = questionGroupId;
                        if (currentGroup != null)
                        {
                            groups.Add(currentGroup);   
                        }
                        currentGroup = new Questionaire();
                        currentGroup.Question_Group_Id = Convert.ToInt32(reader["Question_Group_Id"]);
                        currentGroup.Question_Group_Indent = Convert.ToInt32(reader["Question_Group_Indent"]);
                        currentGroup.Question_Group_Name = reader["Question_Group_Name"].ToString();
                        currentGroup.Question_Group_Order = Convert.ToInt32(reader["Question_Group_Order"]);
                    }
                    if (reader["Question_ID"] != DBNull.Value) 
                    {
                        QuestionaireLine question = new QuestionaireLine();
                        question.Question_ID = Convert.ToInt32(reader["Question_ID"]);
                        question.Question_Answer = reader["Question_Answer"].ToString();
                        question.Issue_Tagged = Convert.ToBoolean(reader["Issue_Tagged"]);
                        question.Data_Type = Convert.ToInt32(reader["Data_Type"]);
                        question.Data_Max_Length = Convert.ToInt32(reader["Data_Max_Length"]);
                        question.Data_Choices = reader["Data_Choices"].ToString();
                        question.Question_Label = reader["Question_Label"].ToString();
                        question.Question_Order = Convert.ToInt32(reader["Question_Order"]);
                        question.Control_Type = Convert.ToInt32(reader["Control_Type"]);
                        currentGroup.Lines.Add(question);
                    }
                    if (currentGroup != null)
                    {
                        groups.Add(currentGroup);
                    }
                }

                reader.Close();
                com.Dispose();
                return groups;
            }
4

1 回答 1

0

您在实例Lines上的属性为 Null。Questionaire改成:

 public class Questionaire
    {
        public int Question_Group_Id { get; set; }
        public string Question_Group_Name { get; set; }
        public int Question_Group_Indent { get; set; }
        public int Question_Group_Order { get; set; }
        public List<QuestionaireLine> Lines { get; set; }

        public Questionaire() {
            Lines = new List<QuestionaireLine>();

    }

顺便说一句,单步执行您的代码会向您展示这一点。

于 2013-01-22T14:33:46.593 回答