0

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

like this What's the error in this code?

Fahad Hassan
  • 581
  • 3
  • 15
  • No, this is not the answer for me – Fahad Hassan Feb 04 '20 at 14:26
  • What do you think would happen if `convObject["createdOn"]` returned `null`? – Igor Feb 04 '20 at 14:27
  • But in my case it is printing this `Console.WriteLine("date : "+Convert.ToDateTime (convObject["createdOn"].ToString()));` that means `convObject["createdOn"]` is not returning null – Fahad Hassan Feb 04 '20 at 14:28
  • Your problem description stated that it prints "till" (which means until but not including). If it does print that line and that value is not null then you need to include the stack trace of the exception in your above question. – Igor Feb 04 '20 at 14:32
  • @Igor there is no stack trace is coming on console. I am just receiving this error `Object reference not set to an instance of an object` – Fahad Hassan Feb 04 '20 at 14:33
  • All exceptions have a stack trace. See also the marked duplicate for further help or google how to get a stack trace from an exception (it is a property of System.Exception). – Igor Feb 04 '20 at 14:35
  • @Igor you can see in my code that i used proper `try` and `catch` block and also prints `Exception` message but still i am not getting exception stack trace. – Fahad Hassan Feb 04 '20 at 14:36
  • You can also print out an exception using `.ToString()` which will include the stack trace as well as nested exception information. – Igor Feb 04 '20 at 14:36
  • `.Message` only gets the message, this omits all the "good" details about the exception including the stack trace and any nested excption information. I would recommend you do a little reading on exception handling best practices. – Igor Feb 04 '20 at 14:37
  • So if i will do like `ex.Message.ToString()` so will it not give me proper stack trace? – Fahad Hassan Feb 04 '20 at 14:38
  • Just `ex.ToString()`. – Igor Feb 04 '20 at 14:39
  • i tried it `Just ex.ToString()` but stil i am only getting this message `Object reference not set to an instance of an object` – Fahad Hassan Feb 04 '20 at 14:42

0 Answers0