I have a task in asp.net core 2.2
which is called eventually on specific condition which is this
private async Task calculateChatDuration (string id)
{
Console.WriteLine("in chat duration helper : "+id);
try {
Console.WriteLine("in try block");
var convObject = DbService.conversations.Find (new BsonDocument ("_id", new ObjectId(id))).FirstOrDefault ();
Console.WriteLine("conv object : " + convObject);
Console.WriteLine("nsp : "+convObject["nsp"].ToString());
Console.WriteLine("email : "+convObject["agentEmail"].ToString());
Console.WriteLine("date : "+Convert.ToDateTime (convObject["createdOn"].ToString()));
ChatDuration chatduration = new ChatDuration();
chatduration.nsp = convObject["nsp"].ToString ();
chatduration.convIDs.Add(id);
chatduration.agentEmail = convObject["agentEmail"].ToString ();
chatduration.date = Convert.ToDateTime (convObject["createdOn"].ToString ());
TimeSpan difference = Convert.ToDateTime (convObject["endingDate"].ToString ()).ToUniversalTime () - Convert.ToDateTime (convObject["createdOn"].ToString ()).ToUniversalTime ();
Console.WriteLine("time difference : "+difference.TotalMinutes);
chatduration.duration = Convert.ToDouble (difference.TotalMinutes);
Console.WriteLine ("inserting jobect in end : " + chatduration.ToJson());
await DbService.warehouse_chatduration.InsertOneAsync(chatduration);
Console.WriteLine ("-- document inserted (chatduration to warehouse) --");
} catch (Exception ex) {
Console.WriteLine (ex.Message);
Console.WriteLine ("Error in Inserting (chatduration to warehouse)");
}
}
The problem is this when this task
called so it prints till
Console.WriteLine("date : "+Convert.ToDateTime (convObject["createdOn"].ToString()));
And throws an error at
ChatDuration chatduration = new ChatDuration();
It throws this error
Object reference not set to an instance of an object
Here is class
definition
public class ChatDuration
{
public BsonObjectId _id {get; set;}
public string nsp {get; set;}
public string agentEmail {get; set;}
public double duration {get; set;}
public DateTime date {get; set;}
public BsonArray convIDs {get; set;}
}
Here is the console results i am getting