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

7
  • 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. Commented Sep 7, 2016 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 ? Commented Sep 7, 2016 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. Commented Sep 7, 2016 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 ! Commented Sep 7, 2016 at 14:55
  • How is your view model instantiated? Commented Sep 7, 2016 at 14:59

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.