Click here to Skip to main content
14,691,000 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team

I am struggling to create check box on my asp.net mvc, im getting this null reference exception that is not set to an instance below is my logic to get the list of those within model and controller class.

What I have tried:

public class EditTrainingRegFormViewModel
   {
       public string Title { get; set; }

       public string FirstName { get; set; }

       public string LastName { get; set; }

       public string Position { get; set; }

       public string Company { get; set; }

       public string Address { get; set; }

       [Display(Name = "Choose country")]
       public int ? SelectedCountryId { get; set; }

       public string Code { get; set; }

       public string City { get; set; }

       public string State { get; set; }

       public string Cell_Number { get; set; }

       public List<CheckBoxes> Dietary_requirement { get; set; }

       public string Email { get; set; }

       public int Country_Id { get; set; }

       public string Country_Name { get; set; }


   }

   public class CheckBoxes
   {
       public string Text { get; set; }
       public bool Checked { get; set; }
   }


// GET://Manage/Check box for Dietary-Req

public ActionResult DietReq()
{
    EditTrainingRegFormViewModel model = new EditTrainingRegFormViewModel();

    model.Dietary_requirement = new List<CheckBoxes>
    {
        new CheckBoxes {Text = "None"},
        new CheckBoxes {Text = "Vegetarian"},
        new CheckBoxes {Text = "Vegan"},
        new CheckBoxes {Text = "Halaal"}
    };

    return View(model);
}


<form>
                     <div class="row">
                         <label for="Dietary Requirements">Dietary Requirements</label>
                         <div class="input-group col-md-6 col-md-offset-3 col-sm-6 col-xs-6">
                             <div class="input-group pull-left">
                                 <!---Iteration for checking each name from the list of available diets-->
                                 @for(int i = 0; i<Model.Dietary_requirement.Count; i++) /* This gets thrown here that is not set to an instance of an object*/
                                 {
                                     @Html.HiddenFor(m=>m.Dietary_requirement[i].Text)
                                     @Html.CheckBoxFor(m=>m.Dietary_requirement[i].Checked, new {id = string.Format("dietary_requirement_{0}", i) })
                                 }
                             </div>
                         </div>
                     </div>
                  </form>
Posted
Updated 17-Mar-20 22:52pm
Comments
Richard MacCutchan 18-Mar-20 4:20am
   
And where does the error occur? How many times do we need to remind you to provide complete details of the problem?
gcogco10 18-Mar-20 4:25am
   
@for(int i = 0; i
F-ES Sitecore 18-Mar-20 6:32am
   
Do you get the problem when the page loads, or when you submit the form? You might want to google how to use checkboxes in MVC.
gcogco10 18-Mar-20 6:33am
   
Every time when I try to load the pages. I get thrown

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900