I'm running into an issue I can't seem to find the solution to, simply because I don't understand how to fix this NullReferenceException.
I have my constructor;
public MainViewModel()
{
this.Refresh = new DelegateCommand(this.DoRefresh);
//...More like this...
//...and finally...
this.InitializeObjects();
}
then somewhere between properties there is the dependency
[Dependency]
public IUnityContainer Container { get; set; }
and finally the InitializeObjects-method generating the NullReferenceException on 'Container'
private void InitializeObjects()
{
using (var context = this.Container.Resolve<IDbContextScope>())
{
//...remainder of the method...
}
}
The exception is thrown at the 3rd row of this block of code, the row starting with 'using (var ...'
The exception is an ArgumentNullException;
Message "Value cannot be nul.Parameter name: container"
Source = Microsoft.Practices.Unity
StackTrace = at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve....etc..
So my concrete questions are; Is it indeed the IUnityContainer Container throwing the exception? Why does it throw the exception? How do I work around this?
Edit:
As found in the first 2/3 comments under the post, the cause of the NullReferenceException is asserted. However, I still don't know how to work around it, as I don't experience this as your every-day NRE. The function needing the Container is there to initialize the values the program needs to function, and therefore needs to be called INSIDE the constructor. AFAIK I can't just declare the dependency, so how do I work around this..?
Resolveis an extension method with a signature likeResolve(this UnityContainer container). The problem is thatpublic IUnityContainer Container { get; set; }isnull. This isn't magically configured somehow. You need Unity to setupMainViewModelin order to satisfy your Container dependency. Whoever instantiates your main view model is responsible for doing this.public MainViewModel(IUnityContainer muhContainer), and remove the [Dependency] property. I'm reopening because I think your problem isn't the NRE, it's how to use Unity.