This is not something I don't know what is NullRefrenceException
. There is some uncertain situation where I was getting the error.
I'm trying to assign values to properties of some classes.
Here are my classes.
// These classes are in EmailProcess namspace
public class ActionedEmailReport
{
public Message Message { get; set; }
public string SaveToSentItems { get; set; }
}
public class ToRecipient
{
public EmailObject.EmailAddress EmailAddress { get; set; }
}
public class Message
{
public string Subject { get; set; }
public Body Body { get; set; }
public List<ToRecipient> ToRecipients { get; set; }
}
public class Body
{
public string ContentType { get; set; }
public string Content { get; set; }
}
// Below class in EmailObject namespace.
namespace EmailObject
{
public class EmailAddress
{
public string Address { get; set; }
}
}
Here is my code to assign values to the properties of the classes.
public void EmailProcessing(string recepeint)
{
ActionedEmailReport actionedReport = new ActionedEmailReport();
List<ToRecipient>toRecipient = new List<ToRecipient>();
EmailObject.EmailAddress emailAddress= new EmailObject.EmailAddress();
emailAddress.Address = recepeint;
toRecipient.Add(new ToRecipient()
{
EmailAddress=emailAddress
});
// I'm getting error on the below line.
actionedReport.Message.ToRecipients = toRecipient;
actionedReport.Message.Body.Content = "Hello";
actionedReport.Message.Body.ContentType = "Text";
actionedReport.SaveToSentItems = "True";
actionedReport.Message.Subject = "Demo Email"
}
On the line actionedReport.Message.ToRecipients = toRec;
I am getting error that
System.NullReferenceException:Object reference not set to an instance of an object.
I have checked inputs properly and sure that I am assigning some value to toRecipient
then why I am getting this error. This is driving me crazy.