c# - Trying to pass value into index of my Object gives an Error "System.NullReferenceException: Object reference not set to an instance of an object." -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
i trying pass value in index of object, gives me error.
system.nullreferenceexception: object reference not set instance of object.
i creating following object index :
recipientinfo[] ri = new recipientinfo[1]; ri[0].email = "email-id"; ri[0].role = recipientrole.signer; if want see recipientinfo method, providing method below.
public partial class recipientinfo { private string emailfield; private system.nullable<recipientrole> rolefield; /// <remarks/> [system.xml.serialization.xmlelementattribute(isnullable = true)] public string email { { return this.emailfield; } set { this.emailfield = value; } } /// <remarks/> [system.xml.serialization.xmlelementattribute(isnullable = true)] public system.nullable<recipientrole> role { { return this.rolefield; } set { this.rolefield = value; } } } why getting error?
your array doesn't have in - it's initialised empty, each position in null. need create recipientinfo before setting properties on it.
simplest change:
recipientinfo[] ri = new recipientinfo[1]; ri[0] = new recipientinfo(); ri[0].email = "email-id"; ri[0].role = recipientrole.signer; or, nicer:
var ri = new recipientinfo[1]; ri[0] = new recipientinfo { email = "email-id", role = recipientrole.signer };
Comments
Post a Comment