I have this simple application to populate fields from a data model, The data model is called “Order” which has a nested class called “Address”. When I attempt to assign a value to “Order.Address.city”, I get the error message “Object reference not set to an instance of an object.” I appreciate any help, thanks!
I tried various different ways to correct the error, Order class doesn’t seem to recognize the Address class.
Data Model is listed below….
namespace ConsoleApp1.DataModels
{
public class Order {
public string name { get; set; }
public string description { get; set; }
public Address address { get; set; }
public class Address
{
public string city { get; set; }
public string state { get; set; }
public string zip { get; set; }
}
}
}
c# code is below and error occurs when this line is executed: mtorder.address.city = “Chicago”
using ConsoleApp1.DataModels;
namespace WalmartOrderProcessing
{
class Program
{
static void Main(string[] args)
{
Order myOrder = new Order();
myOrder.name = "John Doe";
myOrder.description = "Contractor";
myOrder.address.city = "Chicago";
myOrder.address.state = "IL";
myOrder.address.zip = "12345";
}
}
}
Jake_ss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.