0

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..?

Ciphra
  • 269
  • 2
  • 17
  • `Resolve` is an extension method with a signature like `Resolve(this UnityContainer container)`. The problem is that `public IUnityContainer Container { get; set; }` is `null`. This isn't magically configured somehow. You need Unity to setup `MainViewModel` in order to satisfy your Container dependency. Whoever instantiates your main view model is responsible for doing this. –  Sep 07 '16 at 14:38
  • @Will so concretely this means I cannot call the InitializeObjects() function (which utilizes the IUnityContainer) within the constructor of the MainViewModel(), because the IUnityContainer is not yet set to an actual value and remains null until the constructor finishes 'constructing' the MainViewModel ? – Ciphra Sep 07 '16 at 14:45
  • 99% of the way there, yes. Usually, if an object has a marked dependency, then an instance of that object should be retrieved from a Unity container. If you're doing that, perhaps you should use constructor injection in order for the dependency to be available during execution of the constructor? `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. –  Sep 07 '16 at 14:50
  • @Will I was just busy editing the post to 'prove' it wasn't a duplicate question :^) I created the question because I was 99% sure WHAT the problem was, just not HOW to solve it. Should have worded that more destinctively I suppose. I will try and read up on constructor injection to see if I can use that to solve the problem. Thanks for not abandoning me on the duplicate-accusation ! – Ciphra Sep 07 '16 at 14:55
  • How is your view model instantiated? –  Sep 07 '16 at 14:59
  • If you post your second comment as an answer I can give you the credit for it. I thought it would be more complicated but after some research really all it took was what you wrote down. I removed the [dependency] brackets from the property decleration and initialized it whithin the constructor through the parameter. Thanks for the help! – Ciphra Sep 07 '16 at 15:10
  • Chicken/egg with Unity. Always fun. You got it. –  Sep 07 '16 at 15:11

1 Answers1

1

The problem with dependency properties like this

[Dependency]
public IUnityContainer Container { get; set; }

is that they aren't available within the constructor. If you must use this value within the constructor, use a constructor dependency

public MainViewModel(IUnityContainer muhContainer, SomeOtherDependency derp)
{
    // use muhContainer and derp here
}

In general if your object MUST HAVE a dependency, it should be supplied via constructor injection. If you have a dependency that has an acceptable default, but that you might want to change at runtime via configuration, then property injection is fine.

[Dependency]
public Herp WhoCares 
{
    get { return _herp ?? _defaultHerpDoesntMatterLol; }
    set { _herp = value; }
}