Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 481.5k

System.ArgumentNullException:Object reference not set?

Mar 18 2020 2:51 AM
Hi Team
 
I am getting this exception "Object reference not set to an instance of an object". I am trying to create a checkbox within a form using asp.net mvc. Below is my logic and need some help around this issue to fix it, please mates.
  1. public class EditTrainingRegFormViewModel  
  2.    {  
  3.        public string Title { getset; }  
  4.   
  5.        public string FirstName { getset; }  
  6.   
  7.        public string LastName { getset; }  
  8.   
  9.        public string Position { getset; }  
  10.   
  11.        public string Company { getset; }  
  12.   
  13.        public string Address { getset; }  
  14.   
  15.        [Display(Name = "Choose country")]  
  16.        public int ? SelectedCountryId { getset; }  
  17.   
  18.        public string Code { getset; }  
  19.   
  20.        public string City { getset; }  
  21.   
  22.        public string State { getset; }  
  23.   
  24.        public string Cell_Number { getset; }  
  25.   
  26.        public List<CheckBoxes> Dietary_requirement { getset; }  
  27.   
  28.        public string Email { getset; }  
  29.   
  30.        public int Country_Id { getset; }  
  31.   
  32.        public string Country_Name { getset; }  
  33.   
  34.   
  35.    }  
  36.   
  37.    public class CheckBoxes  
  38.    {  
  39.        public string Text { getset; }  
  40.        public bool Checked { getset; }  
  41.    }  
  42.   
  43.        // GET://Manage/Check box for Dietary-Req  
  44.   
  45.        public ActionResult DietReq()  
  46.        {  
  47.            EditTrainingRegFormViewModel model = new EditTrainingRegFormViewModel();  
  48.   
  49.            model.Dietary_requirement = new List<CheckBoxes>  
  50.            {  
  51.                new CheckBoxes {Text = "None"},  
  52.                new CheckBoxes {Text = "Vegetarian"},  
  53.                new CheckBoxes {Text = "Vegan"},  
  54.                new CheckBoxes {Text = "Halaal"}  
  55.            };  
  56.   
  57.            return View(model);  
  58.        }  
  59.   
  60.  <form>  
  61.                       <div class="row">  
  62.                           <label for="Dietary Requirements">Dietary Requirements</label>  
  63.                           <div class="input-group col-md-6 col-md-offset-3 col-sm-6 col-xs-6">  
  64.                               <div class="input-group pull-left">  
  65.                                   <!---Iteration for checking each name from the list of available diets-->  
  66.                                   @for(int i = 0; i<Model.Dietary_requirement.Count; i++) // Error is thrown here as not set to an instance.  
  67.                                   {  
  68.                                       @Html.HiddenFor(m=>m.Dietary_requirement[i].Text)  
  69.                                       @Html.CheckBoxFor(m=>m.Dietary_requirement[i].Checked, new {id = string.Format("dietary_requirement_{0}", i) })  
  70.                                   }  
  71.                               </div>  
  72.                           </div>  
  73.                       </div>  
  74.                    </form>  
 

Answers (7)