zeeshan akram

zeeshan akram

  • 1.1k
  • 325
  • 16.6k

object Reference Not set to and Intense of an Object in mv5

Nov 3 2019 1:54 AM
I am using repository pattern in mvc 5 I have created the ICountry Interface and clsCountry class when run the project and put a break point then _ICountry.listofCountry() throw the error "Object reference not send to an Instance". Any expert can tell me where i am wrong . I am using Database first approch and my db extension is .edmx
Model Countries
  1. public int CountryID { getset; }  
  2. public string CountryName { getset; }  
IcountryRepository
  1. public interface ICountry {  
  2. List<Countries> listofCountry();  
  3. }  
clsCountryRepository
  1. public class clsCountry: ICountry {  
  2.  ECommercedbEntities _db;  
  3.  public clsCountry(ECommercedbEntities db) {  
  4.   _db = db;  
  5.  }  
  6.  public List < Countries > listofCountry() {  
  7.   return _db.Countries.ToList();  
  8.  }  
  9. }
Controller
  1. public class AccountController: Controller {  
  2.  ICountry _ICountry;  
  3.  public AccountController(ICountry ICountry) {  
  4.    _ICountry = ICountry;  
  5.   }  
  6.   [HttpGet] public ActionResult Registration() {  
  7.    try {  
  8.     List < Countries > listCountry = _ICountry.listofCountry();  
  9.     ViewBag.CountryList = new SelectList(listCountry, "CountryID""CountryName");  
  10.     return View();  
  11.    } catch (Exception ex) {  
  12.     throw ex;  
  13.    }  
  14.   }  
  15. }  
AppStart
  1. public static class UnityConfig {  
  2.  public static void RegisterComponents() {  
  3.   var container = new UnityContainer();  
  4.   container.RegisterType < ILogin, clsLogin > ();  
  5.   container.RegisterType < ICountry, clsCountry > ();  
  6.   DependencyResolver.SetResolver(new UnityDependencyResolver(container));  
  7.  }  
  8. }  
Global.asax
  1. protected void Application_Start()  
  2. {  
  3.     UnityConfig.RegisterComponents();  
  4. }  

Answers (3)