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:

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

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -