views:

440

answers:

1

I'm starting a new Silverlight project at the moment, and I'm having issues where by Unity is throwing an exception if my ViewModel (which it is instantiating for me) contains the RaisePropertyChanged event.

I looks like this:

public class AddNewClientViewModel : ViewModelBase {

    private Visibility _extraClientFieldsVisible;
    public Visibility ExtraClientFieldsVisible {
        get {
            return _extraClientFieldsVisible;
        }
        set {
            _extraClientFieldsVisible = value;
            base.RaisePropertyChanged("ExtraClientFieldsVisible");
        }
    }


public AddNewClientViewModel(IMyInterface blah) {


       ExtraClientFieldsVisible = Visibility.Collapsed;
}

ViewModelBase which it inherits looks like this:

    public abstract class ViewModelBase : INotifyPropertyChanged {

    public event PropertyChangedEventHandler PropertyChanged;

    public  void RaisePropertyChanged(string propertyName) {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }


}

The full stack trace is noted below, but it seems like calling the RaisePropertyChanged event during the constructor causes Unity to blowup.

Here's the full stack trace of the error:

Microsoft.Practices.Unity.ResolutionFailedException was unhandled by user code
Message="Resolution of the dependency failed, type = \"ClientSide.ViewModels.AddNewClientViewModel\", name = \"\". Exception message is: The current build operation (build key Build Key[ClientSide.ViewModels.AddNewClientViewModel, null]) failed: Object reference not set to an instance of an object. (Strategy type BuildPlanStrategy, index 3)"
TypeRequested="AddNewClientViewModel"
 StackTrace:
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name)
   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name)
   at Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t)
   at Microsoft.Practices.Unity.UnityContainerBase.Resolve[T]()
   at ClientSide.Framework.ServiceLocator.get_AddNewClientViewModel()
InnerException: Microsoft.Practices.ObjectBuilder2.BuildFailedException
   Message="The current build operation (build key Build Key[ClientSide.ViewModels.AddNewClientViewModel, null]) failed: Object reference not set to an instance of an object. (Strategy type BuildPlanStrategy, index 3)"
   BuildKey="Build Key[ClientSide.ViewModels.AddNewClientViewModel, null]"
   ExecutingStrategyIndex=3
   ExecutingStrategyTypeName="BuildPlanStrategy"
   StackTrace:
        at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
        at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
        at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
   InnerException: System.NullReferenceException
        Message="Object reference not set to an instance of an object."
        StackTrace:
             at ClientSide.ViewModels.ViewModelBase.RaisePropertyChanged(String propertyName)
             at ClientSide.ViewModels.AddNewClientViewModel.set_ExtraClientFieldsVisible(Visibility value)
             at ClientSide.ViewModels.AddNewClientViewModel..ctor(IDataCore dataCore)
             at BuildUp_ClientSide.ViewModels.AddNewClientViewModel(IBuilderContext )
             at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
             at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
             at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
        InnerException:

So I'm hitting a NullReferenceException. I can't just work out how...

+2  A: 

Why not just check for null in RaisePropertyChanged()?

After all, it's not going to cause adverse side effects...

Vijay Patel
I am indeed.. an idiot.. been staring at this for far too long .. thanks.
DaRKoN_
Don't worry - we've all been there :)
Vijay Patel