Object reference not set in MVC 5 object instance

I am new to MVC working on a 3 tier MVC project and I am using a pre-made database.

now I need to write a query using linq in the Business Layer to list doctors like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoctorsSheet.DataAccess;

namespace DoctorsSheet.Business
{
    class Doctor : IDoctor
    {
        DoctorsSheetDBEntities db = new DoctorsSheetDBEntities();

        public IQueryable<Doctors> GetDoctors()
        {
            var doctors = from d in db.Doctors
                          select d;

            return doctors.AsQueryable<Doctors>();
        }
     }
}

      

and when i call GetDoctors () from DoctorsController it tells me that the object reference is not set to an object instance

this is the Controller:

public ActionResult Index()
{
    var doctors = obj.GetDoctors().AsQueryable<Doctors>();
    return View(doctors);
}

      

please help me how to fix this.

+3


source to share


1 answer


Make your class public

-

public class Doctor : IDoctor

      

And then initialize the variable obj

as shown below and then use obj

.



IDoctor obj = new Doctor();

      

NOTE. As @Sippy explained you don't need to use GetDoctors().AsQueryable<Doctors>();

.

+3


source







All Articles