1

I try to fill my model that is member of view model, I send data from Action to view.It's work correct. but when click on save button, get an error that

Object reference not set to an instance of an object.

The view model is

public class OrderCustomer
{
    public Order Order { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
}

And action for post data is

        [HttpPost]
    public ActionResult AddressAndPayment(OrderCustomer values)
    {
        var orderCustomer = new OrderCustomer();
        var order = orderCustomer.Order;

        string uId = User.Identity.GetUserId();
        order.Username = User.Identity.Name;
        order.ApplicationUserId = uId;
        order.OrderDate = DateTime.Now;
        order.Address = values.ApplicationUser.Address;
        order.CityId = (int)values.ApplicationUser.CityId;
        order.CountryId = (int)values.ApplicationUser.CountryId;
        order.Email = values.ApplicationUser.Email;
        order.FirstName = values.ApplicationUser.FName;
        order.LastName = values.ApplicationUser.LName;
        order.Phone = values.ApplicationUser.Phone;
        order.PostalCode = values.ApplicationUser.PostalCode;
        order.ProvinceId = (int)values.ApplicationUser.ProvinceId;

        if (ModelState.IsValid)
        {

            TryUpdateModel(order);

            try
            {
                _db.Orders.Add(order);
                _db.SaveChanges();
                var cart = ShoppingCart.GetCart(this.HttpContext);
                cart.CreateOrder(order);

                return RedirectToAction("Index", "Payment", new { id = order.Id });

            }
            catch
            {
               return View(order);
            }
        }
        return View(order);


    }

when debugger arrive on this code show the error line 5 in above code that show in below, how can i fix it?

        order.Username = User.Identity.Name;
sunny
  • 2,670
  • 5
  • 24
  • 39
  • maybe the request is not authenticated so there is no identity set - look into your auth code or set an anonymous principal like this https://stackoverflow.com/questions/2893334/invalidating-asp-net-formsauthentication-server-side – mchan Aug 31 '17 at 18:39
  • `order` is `null`. you do not assign an instance to that property – Nkosi Aug 31 '17 at 18:41

1 Answers1

2

Given that OrderCustomer does not initialize Order when it is initialized

like

public class OrderCustomer {

    public OrderCustomer () {
        Order = new Order();
    }

    public Order Order { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
}

then Order would be null by default.

This would mean that currently you create an instance of OrderCustomer but never assign a value to the Order property.

 var orderCustomer = new OrderCustomer();
 var order = orderCustomer.Order; //<-- Order is null by default

Thus order is null as you do not assign a object to that variable.

So either refactor to example above or make sure to create a new instance.

var orderCustomer = new OrderCustomer() {
    Order = new Order()
};

var order = orderCustomer.Order;
//...code removed for brevity

This however now begs the question why you are not assigning that value from what was posted as part of the view model?

I believe this is what you intended

var order = values.Order;
Nkosi
  • 235,767
  • 35
  • 427
  • 472