Search code examples
c#asp.netoopc#-4.0echosign

Trying to pass value into index of my Object gives an Error "System.NullReferenceException: Object reference not set to an instance of an object."


I am trying to pass a value in Index of my object, it gives me an error.

System.NullReferenceException: Object reference not set to an instance of an object.

I am creating the following object with index :

RecipientInfo[] RI = new RecipientInfo[1];
RI[0].email = "email-id";
RI[0].role = RecipientRole.SIGNER;

If you want see my RecipientInfo method, providing you the method below.

public partial class RecipientInfo
{
    private string emailField;
    private System.Nullable<RecipientRole> roleField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
    public string email
    {
        get { return this.emailField; }
        set { this.emailField = value; }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
    public System.Nullable<RecipientRole> role
    {
        get { return this.roleField; }
        set { this.roleField = value; }
    }
}

Why am I getting this error?


Solution

  • Your array doesn't have anything in it - it's initialised empty, each position in it will be null. You need to create a 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, slightly nicer:

    var RI = new RecipientInfo[1];
    
    RI[0] = new RecipientInfo
    {
        email = "email-id", 
        role = RecipientRole.SIGNER
    };