question

OulcanAkca-9475 avatar image
0 Votes"
OulcanAkca-9475 asked ·

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

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; }
     }




sql-server-generaldotnet-csharpdotnet-entity-framework
· 4
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Probably the second constructor of ManagerPanelInformationManager was called with null parameter, or was not called at all. It is not clear how these interface and classes are instantiated.

0 Votes 0 ·

Well, how can i fix this?

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 OulcanAkca-9475 ·

Do you have a class that implements the IManagerPanelInformationDal interface?

Probably you are using a sophisticated apparatus where ‘new’ is banned, therefore you have to spend time clarifying how to initialise _managerPanelInformationDal with some object (probably indirectly). Do you have some code or configuration file where you can say something like ‘for IManagerPanelInformationDal interface use my ManagerPanelInformationDal class’?

Or maybe remove the first parameter-less constructor of ManagerPanelInformationManager and make the second constructor public.

Show some more details; how did you achieve the problematic line?


0 Votes 0 ·
Show more comments
Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

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();

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I can't believe how I ignored this. Thank you for helping. My problem was solved.

0 Votes 0 ·
APoblacion avatar image
0 Votes"
APoblacion answered ·

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.

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I put a break point and I did not understand with which section I have done fault. I also have the second form with the same events and methods in the same project. Everything works smoothly there. Could I have made a linking mistake here? Can I upload a project? Maybe you can be helped more easily?

0 Votes 0 ·

Hi @OulcanAkca-9475,
Based on your code, there is no code for calling a parameterized constructor(internal ManagerPanelInformationManager(IManagerPanelInformationDal iManagerPanelInformationDal)).
So" _managerPanelInformationDal" is not assigned and is null which caused the error you encountered.
I suggest you assign a value to "_managerPanelInformationDal" to test it.
>> also have the second form with the same events and methods in the same project. Everything works smoothly there. Could I have made a linking mistake here?
Are the two forms the same code, or please add more detailed code.
Best Regards,
Daniel Zhang

0 Votes 0 ·

I shared it below.

0 Votes 0 ·
OulcanAkca-9475 avatar image
0 Votes"
OulcanAkca-9475 answered ·

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


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.