Hi
I am getting error - Object reference not set to an instance of the object.
objShippingOrdr.consignee.name = txtConsigneeName.Text;
Below is the code
C#
public class ModelClass
{
public string OrderNo { get; set; }
public int shipping_charges { get; set; }
public int discount { get; set; }
public int cod_charges { get; set; }
public string PaymentType { get; set; }
public decimal OrderAmount { get; set; }
public Consignee consignee { get; set; }
}
public class Consignee
{
public string name { get; set; }
public string address { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public Int32 pincode { get; set; }
public Int32 phone { get; set; }
}
public void CreateShippingOrder()
{
var objShippingOrdr = new ModelClass();
objShippingOrdr.OrderNo = txtOrderNo.Text;
objShippingOrdr.PaymentType = ddlPaymentType.SelectedItem.Value;
objShippingOrdr.OrderAmount = Convert.ToDecimal(txtOrderAmount.Text);
objShippingOrdr.consignee.name = txtConsigneeName.Text;
objShippingOrdr.consignee.address = txtConsigneeAddress.Text;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
CreateShippingOrder();
}Thanks

Amit MohantyRank #112 · 20K reputationPosted Dec 22, 2022, 2:21 AM EST
You have to create instance of Consignee class before assing value.
objShippingOrdr.consignee = new Consignee(); objShippingOrdr.consignee.name = txtConsigneeName.Text; objShippingOrdr.consignee.address = txtConsigneeAddress.Text;Vishal JoshiRank #377 · 6.7K reputationPosted Dec 22, 2022, 2:27 AM EST
Hello
Define base class with constructor to initialize it's property. please change base class code with below.
public class ModelClass { public ModelClass() { consignee = new Consignee() } public string OrderNo { get; set; } public int shipping_charges { get; set; } public int discount { get; set; } public int cod_charges { get; set; } public string PaymentType { get; set; } public decimal OrderAmount { get; set; } public Consignee consignee { get; set; } }Thanks