Search code examples
asp.net-mvc-3modelado.netdatareader

Troubleshoot object reference error when filling mvc models with datareader?


I am using MVC 3 and I am using a datareader to create a list of items that have subItems. When I add the subitem I get "Object reference not set to an instance of an object." With the following code:

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

The models:

 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); }
         }
    }

The whole code: // what am I missing??

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;
            }

Solution

  • Your Lines property on your Questionaire instance is Null. Change to:

     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>();
    
        }
    

    b.t.w. stepping through your code would have shown you that.