Skip to content

An exception error

Siyabonga Mthethwa

Asked Jul 27, 2015, 12:40 AM EDT1 answer
Share
LinkedInX
i have a above code on my controller, 
Java
 public ActionResult Details(int? id)
        {
            var dbo = new DrugBusiness();
            DrugModel dm = dbo.DetailsMethod(id);

            DateTime drug_expdate = dbo.GetAll().Find(x => x.DrugId == id).ExpiryDate;
            DateTime now = DateTime.Today;
            var days = drug_expdate - now;
            if(drug_expdate<=now)
            {
                ViewBag.msg = "The Drug has expired";
            }
            else
            {
                ViewBag.msg = "This Drug expires in " + days.Days + "Days";
            }
            return PartialView(dbo.DetailsMethod(id));
 
but when im running my Details view it's giving me an error in yellow line, this what it says "{"Object reference not set to an instance of an object."}" 
please help me ive try but nothing is working, please  

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Pranay Rana
    +2
    i suggest you replace this line
     
     
    DateTime drug_expdate = dbo.GetAll().Find(x => x.DrugId == id).ExpiryDate;
     
    with 
     
    Code
    var date = dbo.GetAll().FirstOrDefault(x => x.DrugId == id);
    if(date!=null)
    {
      DateTime drug_expdate = date.ExpiryDate;
    }
     
    I am not sure but you might be getting error because there is no match found.
    +2