0

My code was working fine till yesterday. but now when I am trying to run my code I am getting System.Null Reference Exception. I have no idea why this is happening. I am getting this error on If condition.

My code:

    public void Post(Message message)
    {
        CreateRecordsInCrm c = new CreateRecordsInCrm();


        if (message.Current.pipeline_id == 1)
        {
            c.CheckCondition(message);
        }

    }
}

Message Class :

public class Message
{
    public Current Current { get; set; }
}

public class Current
{
    public string Status { get; set; }
    public string Title { get; set; }
    public string org_name { get; set; }
    public string person_name { get; set; }
    public string cc_email { get; set; }
    public decimal value { get; set; }
    public string owner_name { get; set; }
    public int pipeline_id { get; set; }
    public string person_id { get; set; }
    public DateTime first_won_time { get; set; }
}
vidhi
  • 69
  • 2
  • 9
  • vidhi , use the code from answer below , it will solve your problem , you need to check if message object is null , before checking the value of inside element of message object – Sachin Rajput Jan 07 '18 at 09:22
  • hope , the below answer helps you Vidhi , feel free to upvote answer , happy coding and good luck – Sachin Rajput Jan 07 '18 at 09:37

1 Answers1

1

You need to check if message object is not null , then you can check your current condition

 public void Post(Message message)
        {
            CreateRecordsInCrm c = new CreateRecordsInCrm();
            // Check if message is not null , then next condition 
           if (message != null &&  message.Current != null && message.Current.pipeline_id == 1)
            {
                c.CheckCondition(message);
            } else{
              // message or Current object is null
            }

        }
    }
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
  • 1
    After adding message != null it worked. – vidhi Jan 07 '18 at 09:30
  • 1
    yes , but some time your Current object inside message Object can also be null vidhi , so we need to put check for message and Current both , i am glad to be able to help you – Sachin Rajput Jan 07 '18 at 09:33