Resolved Having trouble with my dependency injection

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
I've implemented a Dependency constructor within my main program which works perfectly, the method is triggered and it logs my data just fine. however when I attempt to achieve the same thing in another class it gives me the following error:

""System.Null.ReferenceException "object not set to an instance of an object"

I've included the private property and constructor in the class ,as I have in my main program but its still giving me an error. The problem is that the "_LoggingData" is set to null. Does anyone know why this is? Thankyou


C#:
// the error is occuring in the code below. I have this exact code in my main program so i dont understand why its throwing an exception

_loggingData.LogEvent($"An error occured opening {fileName} - Reason  :  File read error");


This is the private property and constructor that i've included in my exceptionHandling class (where the exception is being thrown)

C#:
        private static ILoggingData _loggingData;
        public ExceptionHandling(ILoggingData loggingdata) // this is where the error is occuring.
        {
            _loggingData = loggingdata;
        }
 
Last edited:
Are you using and Inversion of Control container framework, or are you manually constructing each instance and taking care of passing in the parameters.

If the former, then that means you have a constructor that allows construction without the logger, or that you registered your classes incorrectly, or that the IoC container has a bug.

If the latter, then that means that you have a constructor that allows construction without the logger, or that you passed in a null for the logger.
 
Are you using and Inversion of Control container framework, or are you manually constructing each instance and taking care of passing in the parameters.

If the former, then that means you have a constructor that allows construction without the logger, or that you registered your classes incorrectly, or that the IoC container has a bug.

If the latter, then that means that you have a constructor that allows construction without the logger, or that you passed in a null for the logger.
im manually constructing each instance and passing in the parameters.
 
Last edited:
Set a breakpoint on your ExceptionHandler's constructor. Who is passing you a null for the loggingData parameter? Or do you have a parameterless ExceptionHandler constructor and that's the one that is being used?
 
Set a breakpoint on your ExceptionHandler's constructor. Who is passing you a null for the loggingData parameter? Or do you have a parameterless ExceptionHandler constructor and that's the one that is being used?
Okay so ive just realised its not working in my main program either now. The _loggingdata private static property is null, which is whats passing the loggingdata parameter a null value. The exceptionHandler class has no no empty constructor either
 
Last edited:
Is your Program class your "main" class? If so, how is the constructor called for your Program class? Were you expecting the compiler to provide an ILoggerData instance for you?

In your static Main method, you can create an instance of the whatever implements ILoggerData, and then create an instance of your Program class passing in that ILoggerData.
 
Is your Program class your "main" class? If so, how is the constructor called for your Program class? Were you expecting the compiler to provide an ILoggerData instance for you?

In your static Main method, you can create an instance of the whatever implements ILoggerData, and then create an instance of your Program class passing in that ILoggerData.
I cannot believe I never saw that o_O. Thank you so much! it all works now!
 
Back
Top Bottom