locked
why IDbset<T> giving me exception? RRS feed

  • Question

  • User140010582 posted

    I have base repository like:

        public abstract class RepositoryBase<T>:IRepository<T> where T:class
        {
            private EmployeeMSystemEntities _dataContext;
            private readonly IDbSet<T> _dbSet;
            protected IDatabaseFactory DatabaseFactory { get; private set; }
            protected RepositoryBase(IDatabaseFactory databaseFactory)
            {
                DatabaseFactory = databaseFactory;
                _dbSet = _dataContext.Set<T>();
            }

    in this line  

    _dbSet = _dataContext.Set<T>();

    I am getting this following exception:

    [NullReferenceException: Object reference not set to an instance of an object.]
       EmployeeManagement.Repository.Abstractions.RepositoryBase`1..ctor(IDatabaseFactory databaseFactory) in C:\Users\sandesh\Desktop\RepositoryAndGenericRepo\EmployeeManagementSystem\EmployeeManagement.Repository\Abstractions\RepositoryBase.cs:22
       EmployeeManagement.Repository.Repo.Implementation.EmployeeRepository..ctor(IDatabaseFactory databaseFactory) in C:\Users\sandesh\Desktop\RepositoryAndGenericRepo\EmployeeManagementSystem\EmployeeManagement.Repository\Repo\Implementation\EmployeeRepository.cs:15
       lambda_method(Closure , Object[] ) +65
       Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() in C:\projects\autofac\src\Autofac\Core\Activators\Reflection\ConstructorParameterBinding.cs:128

    I am just doing my best to solve this problem since 2 days but couldn't solve please help.

    Wednesday, June 24, 2020 5:25 PM

Answers

  • User-474980206 posted

    in the constructor, _dataContext is null. I assume the factory was supposed to initialize it and you forgot this step.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 5:43 PM

All replies

  • User1120430333 posted

    _dbSet = _dataContext.Set<T>();

    You need to set a debug breakpoint on the line and find out which object is a null valued object,  by using Quickwatch.  that has not been instanced into an object.

    And then you need to find out why.

    OO is OO Java or .NET. It's OO 101 being talked about in the link.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Wednesday, June 24, 2020 5:37 PM
  • User-474980206 posted

    in the constructor, _dataContext is null. I assume the factory was supposed to initialize it and you forgot this step.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2020 5:43 PM