How can I solve it mistake? 'Object reference not set to an instance of an object.'

Oğulcan Akca 196 Reputation points
2021-03-03T10:49:16.567+00:00

I have looked at the forum but did not understand. Can you help? I get this error: 'Object reference not set to an instance of an object.'.

public class ManagerPanelInformationManager : IManagerPanelInformationService
    {
        internal IManagerPanelInformationDal _managerPanelInformationDal;
        public ManagerPanelInformationManager()
        {

        }
        internal ManagerPanelInformationManager(IManagerPanelInformationDal iManagerPanelInformationDal)
        {
            _managerPanelInformationDal = iManagerPanelInformationDal;
        }

        public void Add(ManagerPanelInformation managerPanelInformation)
        {
            _managerPanelInformationDal.Add(managerPanelInformation);
        }

        public void Update(ManagerPanelInformation managerPanelInformation)
        {
            _managerPanelInformationDal.Update(managerPanelInformation);
        }

        public void Delete(ManagerPanelInformation managerPanelInformation)
        {
            _managerPanelInformationDal.Delete(managerPanelInformation);
        }



        public List<ManagerPanelInformation> Get()
        {
            return _managerPanelInformationDal.Get();
        }

        public List<ManagerPanelInformation> GetAll()
        {
            return _managerPanelInformationDal.GetAll();
        }

        public List<ManagerPanelInformation> GetManagerPanelInformationByManagerPanelInformationsId(int managerPanelInformation)
        {
            throw new NotImplementedException();
        }
    }

public interface IManagerPanelInformationService
    {
        List<ManagerPanelInformation> GetAll();
        List<ManagerPanelInformation> Get();
        List<ManagerPanelInformation> GetManagerPanelInformationByManagerPanelInformationsId(int managerPanelInformation);
        void Add(ManagerPanelInformation managerPanelInformation);
        void Update(ManagerPanelInformation managerPanelInformation);
        void Delete(ManagerPanelInformation managerPanelInformation);
    }

internal interface IManagerPanelInformationDal : IEntityRepository<ManagerPanelInformation>
    {

    }

public class EfManagerPanelInformationDal : EfEntityRepositoryBase<ManagerPanelInformation, ManagerPanelContext>, IManagerPanelInformationDal
    {

    }

public interface IEntityRepository<T> where T : class, IEntity, new()
    {
        List<T> GetAll(Expression<Func<T, bool>> filter = null);
        void Add(T entity);
        void Update(T entity);
        void Delete(T entity);
        List<T> GetId(Expression<Func<T, bool>> filter = null);
        List<T> Get(Expression<Func<T, bool>> filter = null);

    }

I am getting the error here;

public List<ManagerPanelInformation> GetAll()
        {
            return _managerPanelInformationDal.GetAll();
        }

public void idGetir()
            {


                dgwRules.DataSource = _iManagerPanelInformationService.GetAll();

            }

    public void Ayarlar_Load(object sender, EventArgs e)
            {
               idGetir();

            }

Other things ;

public class ManagerPanelInformation : IEntity
    {
        public int Id { get; set; }
        public int TC { get; set; }
        public string Kurallar { get; set; }
    }

public class ManagerPanelContext : DbContext
    {
        internal DbSet<ManagerPanelInformation> ManagerPanelInformations { get; set; }
    }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,396 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,758 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-03-04T08:33:36.393+00:00

    Maybe follow the reasonable recommendation, suggested by the system: “Ensure that you have defined a binding for IManagerPanelInformationDal”. Try adding the next line to BusinessModule.Load:

    Bind<IManagerPanelInformationDal>().To<EfManagerPanelInformationDal>().InSingletonScope();
    

2 additional answers

Sort by: Most helpful
  1. Alberto Poblacion 1,556 Reputation points
    2021-03-03T18:50:26.813+00:00

    If you are getting the error in this line:
    return _managerPanelInformationDal.GetAll();
    it means that the variable _managerPanelInformationDal is null.
    So, why is it null? Apparently its value is saved in the constructor of ManagerPanelInformationManager, where you set _managerPanelInformationDal = iManagerPanelInformationDal, the last variable being a parameter to the constructor.
    And this, in turn, means that wherever you are calling the constructor you are passing null as a parameter (or a variable whose value is null).
    So, in order to fix it, you need to find the place where you are calling the constructor (it is not visible in the code that you have posted), and examine it to find out why whatever you are passing happens to be null. From the looks of the code, the thing that you should be passing is an instance of a class that implements the IManagerPanelInformationDal interface. Find out why you are not passing such a thing and change it.


  2. Oğulcan Akca 196 Reputation points
    2021-03-04T07:50:28.473+00:00

    My second form is with the same events and methods in the same project ;

    public class ELibraryInformation : IEntity
        {
            public int Id { get; set; }
            public string Ad { get; set; }
            public string Soyad { get; set; }
            public int Telno { get; set; }
            public string Kitapadi { get; set; }
            public string Kitapturu { get; set; }
            public string Kitapyazari { get; set; }
            public int Kitapsayfasayisi { get; set; }
        }
    
    public class EfInformationDal : EfEntityRepositoryBase<ELibraryInformation, ELibraryContext>, IInformationDal
        {
    
        }
    
    
    public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>
             where TEntity : class, IEntity, new()
            where TContext : DbContext, new()
    
        {
    
    
            public void Add(TEntity entity)
            {
                using (TContext context = new TContext())
                {
                    var addedEntity = context.Entry(entity);
                    addedEntity.State = EntityState.Added;
                    context.SaveChanges();
                }
            }
    
            public void Delete(TEntity entity)
            {
                using (TContext context = new TContext())
                {
                    var deletedEntity = context.Entry(entity);
                    deletedEntity.State = EntityState.Deleted;
                    context.SaveChanges();
                }
            }
    
    
    
    
    
    
            public List<TEntity> GetAll(Expression<Func<TEntity, bool>> filter = null)
            {
                using (TContext context = new TContext())
                {
                    return filter == null ? context.Set<TEntity>().ToList() : context.Set<TEntity>().Where(filter).ToList();
                }
            }
    
    
            public List<TEntity> GetId(Expression<Func<TEntity, bool>> filter = null)
            {
                using (TContext context = new TContext())
                {
                    return filter == null ? context.Set<TEntity>().ToList() : context.Set<TEntity>().Where(filter).ToList();
                }
            }
            public List<TEntity> Get(Expression<Func<TEntity, bool>> filter = null)
            {
                using (TContext context = new TContext())
                {
                    return filter == null ? context.Set<TEntity>().ToList() : context.Set<TEntity>().Where(filter).ToList();
                }
            }
    
    
            public void Update(TEntity entity)
            {
                using (TContext context = new TContext())
                {
                    var updatedEntity = context.Entry(entity);
                    updatedEntity.State = EntityState.Modified;
                    context.SaveChanges();
                }
            }
    
        }
    
    
    public interface IInformationDal : IEntityRepository<ELibraryInformation>
        {
    
        }
    
    public class ELibraryContext : DbContext
        {
            public DbSet<ELibraryInformation> ELibraryInformations { get; set; }
        }
    
    public interface IEntity
        {
    
        }
    
    internal class InformationManager : IInformationService
        {
            public IInformationDal _informationDal;
    
    
            public InformationManager()
            {
    
            }
    
            public InformationManager(IInformationDal informationDal)
            {
                _informationDal = informationDal;
            }
    
    
    
            public void Add(ELibraryInformation eLibraryInformation)
            {
                _informationDal.Add(eLibraryInformation);
            }
    
            public void Delete(ELibraryInformation eLibraryInformation)
            {
                try
                {
                    _informationDal.Delete(eLibraryInformation);
                }
                catch
                {
    
                    throw new Exception("Silme gerçekleşemedi");
                }
            }
            public void Update(ELibraryInformation eLibraryInformation)
            {
                _informationDal.Update(eLibraryInformation);
            }
    
    
            public List<ELibraryInformation> GetAll()
            {
                return _informationDal.GetAll();
            }
    
            public List<ELibraryInformation> GetELibraryInformationByELibraryInformationsName(string eLibraryInformationName)
            {
                throw new NotImplementedException();
            }
    
    
    
            public List<ELibraryInformation> GetId()
            {
                return _informationDal.GetId();
            }
    
        }
    
    public interface IInformationService
        {
            List<ELibraryInformation> GetAll();
    
            List<ELibraryInformation> GetELibraryInformationByELibraryInformationsName(string eLibraryInformationName);
            void Add(ELibraryInformation eLibraryInformation);
            void Update(ELibraryInformation eLibraryInformation);
            void Delete(ELibraryInformation eLibraryInformation);
            List<ELibraryInformation> GetId();
    
        }
    
    public class InstanceFactory
        {
            public static T GetInstance<T>()
            {
                StandardKernel kernel = new StandardKernel(new BusinessModule());
                return kernel.Get<T>();
            }
        }
    
    
    public class BusinessModule : NinjectModule
        {
            public override void Load()
            {
                Bind<IInformationService>().To<InformationManager>().InSingletonScope();
                Bind<IInformationDal>().To<EfInformationDal>().InSingletonScope();
                Bind<IManagerPanelInformationService>().To<ManagerPanelInformationManager>().InSingletonScope();
            }
        }
    

    DanielZhang-MSFT
    APoblacion

    0 comments No comments