Object reference not set to an instance of an object

I keep getting this when I try to create a new employee in my application.  

Here's my code: this is on an EmployeeController.cs file,   the problem is on the department.Employees line.

  [HttpPost]
        public ActionResult Create(CreateEmployeeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var department = _db.Departments.Single(d => d.ID == viewModel.DepartmentID);
                var employee = new Employee();
                employee.Name = viewModel.Name;
                employee.HireDate = viewModel.HireDate;
                department.Employees.Add(employee);

                _db.Save();

                return RedirectToAction("detail", "department", new { id = viewModel.DepartmentID });
            }
            return View(viewModel);
        }

I haven't been able to find the issue.  Any help is greatly appreciated.  I'm working on learning this.
SherryDeveloperAsked:
Who is Participating?
 
käµfm³d 👽Commented:
You context isn't instantiating the Employees member. You either need to fix your Department class to properly instantiate Employees (to an empty List), or you need to code defensively in what you've shown.

e.g.

var department = _db.Departments.Single(d => d.ID == viewModel.DepartmentID);
if (department.Employees == null) department.Employees = new List<Employee>();

Open in new window

0
 
SherryDeveloperAuthor Commented:
I wouldn't accept the second line of code you gave.  Does it make the difference that this is a >Net4 MVC Web app?  Here's the code for the whole EmployeeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eManager.Web.Models;
using eManager.Domain;

namespace eManager.Web.Controllers
{
    public class EmployeeController : Controller
    {
        private readonly IDepartmentDataSource _db;

        public EmployeeController(IDepartmentDataSource db)
        {
            _db = db;
        }
       
        [HttpGet]
        public ActionResult Create(int departmentId)
        {
            var model = new CreateEmployeeViewModel();
            model.DepartmentID = departmentId;
           
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CreateEmployeeViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var department = _db.Departments.Single(d => d.ID == viewModel.DepartmentID);
                var employee = new Employee();
                employee.Name = viewModel.Name;
                employee.HireDate = viewModel.HireDate;
                department.Employees.Add(employee);

                _db.Save();

                return RedirectToAction("detail", "department", new { id = viewModel.DepartmentID });
            }
            return View(viewModel);
        }

    }
}

There is also a Create.cshtml and a CreatEmployViewModel.cs  I'm going over some other code to make sure I didn't make a mistake in it.  I'm following a lesson from pluralsight.

Thanks
Sherry
0
 
käµfm³d 👽Commented:
I wouldn't accept the second line of code you gave.
I'm not sure what you mean.

Does it make the difference that this is a >Net4 MVC Web app?
It shouldn't. You may need to make a change to the data context.

I'm following a lesson from pluralsight.
Which one?
0
 
SherryDeveloperAuthor Commented:
Pluralsight ASP.Net MVC 4 Fundamentals

when I put in the code you gave, sorry, I just had to leave the line of code in that I had originally also.
var employee = new Employee();

That seems to have solved one problem.  Now my savechanges need work.  Doesn't like the datatime data type.
resulted in out of range.  So, onward and upward in my learning.

I'll go ahead and accept the solution.  Thank you.  I must have missed something in another file, because the original code worked before.  The only difference is that I'm doing an intranet vs internet app.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.