1

I'm trying to develop a website using asp.net mvc 4 & EF6 where I want to update record and add new record to another model using the values from updated record. So far I can update the existing record but I get error in the controller when system is trying to add new record to another model. The error is,

Object reference not set to an instance of an object.

Here are my codes,

Controller

[HttpPost]
    public ActionResult RentController(FlatModel flatId)
    {
        if (Session["AdminNAME"] != null)
        {
            if (ModelState.IsValid)
            {
                var dbPost = rentdb.FlatInfoes.Where(p => p.flatno == flatId.Flats.flatno).FirstOrDefault();
                if (dbPost == null)
                {
                    return RedirectToAction("RentController");
                }
                dbPost.flat_owner_name = flatId.Flats.flat_owner_name;
                dbPost.flat_owner_phone = flatId.Flats.flat_owner_phone;

                var addRentSchedule = flatId.BillCheck;
                addRentSchedule.fullname = flatId.Flats.flat_owner_name;    //error showing in this line.
                addRentSchedule.isRented = "N";
                addRentSchedule.due = 10000;
                DateTime today = DateTime.Now;
                DateTime newDay = today.AddDays(30);
                addRentSchedule.submitTime = newDay;
                rentdb.BillPayChecks.Add(addRentSchedule);

                rentdb.SaveChanges();
                TempData["flat_assign_success"] = "Information Updated Successfully!";
                return RedirectToAction("RentController");
            }
            else
            {
                TempData["flat_assign_fail"] = "Error! Information update failed!";
                return RedirectToAction("RentController");
            }
        }
        else
        {
            return RedirectToAction("AdminLogin");
        }
    }

Model

public class FlatModel
{
    public FlatInfo Flats { get; set; }
    public BillPayCheck BillCheck { get; set; }
}

Am I doing something wrong in my code? How can I add the record from the updated values when updating the model? Need this help badly. Tnx.

Shihan Khan
  • 2,180
  • 4
  • 34
  • 67
  • 2
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  May 19 '15 at 12:42
  • Check if 'flatId.BillCheck' is not null, if its null then instantiate 'var addRentSchedule' with new object of 'BillPayChecks' – Manik Arora May 19 '15 at 12:42
  • Debug you code. What is the value of `addRentSchedule`? –  May 19 '15 at 12:43
  • I've a `serial` row at the beginning, but it is set to `auto-increment`, is this creating the problem? – Shihan Khan May 19 '15 at 12:45

1 Answers1

1

This is a little bit of a "troubleshoot my code" type question, so getting an accurate answer will be tough. However, looking at this line

var addRentSchedule = flatId.BillCheck;

Makes me think you are getting a Null reference. You are seeing the error on the line right after this one because you are trying to set properties on a null object. If the rent schedule is infact a NEW database record, then most likely, you need to create a new object. So, your code possibly should look something like this:

var addRentSchedule = new BillCheck();
//you need to tie this to the parent record using the parent record
//primary key and setting the BillCheck Foreign key appropriately
//since I dont know your db schema, I am guessing on this line.
addRentSchedule.flatId = flatId.Id;
addRentSchedule.fullname = flatId.Flats.flat_owner_name;
addRentSchedule.isRented = "N";
addRentSchedule.due = 10000;
//no point in creating variables when you can do it all in one line here.
addRentSchedule.submitTime = DateTime.Now.AddDays(30);
rentdb.BillPayChecks.Add(addRentSchedule);
Tommy
  • 39,592
  • 10
  • 90
  • 121
  • Yes your guess is correct. My db is fresh new and creating a new object is the one working. But just want to know, is it safe if I use new object everytime when adding the record in the model? – Shihan Khan May 19 '15 at 12:56
  • 1
    You will have multiple child records for a specific property, but that won't cause issues at the database level. Now, if your specifications say that there should only be one rent schedule per property, then you would want to check if one exists first, if it does not exist, then create new. Else edit the existing one. Hope that helps! – Tommy May 19 '15 at 13:41
  • Tnx a lot for your great response! :) – Shihan Khan May 19 '15 at 13:46