1

Inside a loop I'm trying to add an object with the type Location to a List<Location> property.

CONTROLLER:

 [HttpPost]
 public ActionResult Index([Bind(Include = "CitySearch")]HomeViewModel model)
 {
    List<CityIdentity> ids = null;
    ids = service.GetNamesId(model.CitySearch);

    foreach (var id in ids)
    {
        var loc = service.GetLocation(id.CityId);
        var location = new Location
        {
            CityId = id.CityId,
            Place = loc.Place,
        };
        model.Locations.Add(location);
    }
}

VIEWMODEL:

public class HomeViewModel
{
    public string CitySearch { get; set; }

    public List<Location> Locations { get; set; }

}

model is of type HomeViewModel and property Locations is of type List<Location>. model is instance from form HttpPost action from submitting the form in the View.

I'm debugging and the error's occurs at model.Locations.Add(location) and I'm getting object reference not set to an instance of an object. and nullref exception.

Any idea?

3
  • 2
    Locations probably is null. Probably a problem with your markup that's causing the model binder to miss the type. Commented Jan 8, 2013 at 21:48
  • how is model assigned in the object? Commented Jan 8, 2013 at 21:51
  • it's good to initialize your properties in model's constructor, so you can add : this.Locations = new HashSet<Location>(); Commented Jan 9, 2013 at 0:51

2 Answers 2

2

Based on your reactions and the context, it seems your Locations property isn't initialized at the time, so you can't use a method Add on this collection. To overcome this issue, you need to initialize this property first and then you can manipulate it whatever way you need to.

One way of achieving this is to initialize your Locations property in the constructor of your model (or wherever you see fit).

So just add something like this and you're good to go:

Locations = new List<Location>();

This initializes your property enough to use its methods.

Sign up to request clarification or add additional context in comments.

Comments

2

Any idea?

Yes, either the model variable is null or the Locations property is null. So make sure that they are not null before accessing model.Locations. Otherwise the exception you are getting is perfectly normal. You cannot call methods on a null instance. By the way you could have used standard debugging techniques (putting a breakpoint in your code, running your application in Debug mode and inspecting the values of your variables) would have lead you to the same conclusion.

Now why your model or model.Locations property is null is a hugely different question. I guess that the form you submitted didn't contain input elements that obey the standard naming conventions for binding to a List. I invite you to familiarize yourself with those conventions and respect them if you expect the default model binder to be able to rehydrate your models in the POST actions.

Maybe somehow you put the model as action argument and expected that ASP.NET MVC will automagically populate it from somewhre? No, that's not how ASP.NET MVC works. There's no magic. ASP.NET MVC uses model binders which have strict rules. Only what you send as arguments to the action is what you can expect to get back.

3 Comments

It appears that Locations is null. But I don't understand how to init it. If do this instead: model.Locations = service.GetLastnames("name") - which is returning a List<Location> object. Everything works. How do I initialize Locations?
@user1121487 Is the posted code / issue part of an Action member of a controller class? If so, please post the markup, and the raw post data (Easy to do with Fiddler2).
How do you expect me knowing this? I have strictly no idea how you are supposed to initialize this property. If you have provided some context in your question about what you are trying to do, how your models and views look like, probably I could have answered your question. But right now it's as if you wrote me your question in Chinese (mandarin Chinese, not simplified Chinese). By the way if you expect this property to be populated from values entered in the view I very strongly invite you to read the article I have linked to in my answer and familiarize yourself with the standard convention

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.