NullReference exception in CSLA 3.8.4 Silverlight when using PropertyStatus control and BXF

NullReference exception in CSLA 3.8.4 Silverlight when using PropertyStatus control and BXF

Old forum URL: forums.lhotka.net/forums/t/9577.aspx


Jaans posted on Monday, September 27, 2010

After adding a property status control to my View (which is bound to a ViewModel via a CollectionViewSource as per MVVM video series), I get a null reference exception (deep in the depths of CSLA) when I show the view from the presenter. If I remove the property status this does not happen.

Here's the property status declaration in the View:

<csla:PropertyStatus Grid.Row="0" Grid.Column="2" Property="{Binding Model.Description}" />

Here's the ShowView code from the presenter:

Shell.Instance.ShowView( typeof( Views.Site.LocationGroup ).AssemblyQualifiedName, "ViewModel"new LocationGroupViewModel(), Regions.MainContent );

Here's the full exception stack:

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  StackTrace:
       at Csla.Reflection.MethodCaller.GetPropertyValue(Object obj, PropertyInfo info)
       at Csla.Silverlight.PropertyStatus.GetRealSource(Object source, String bindingPath)
       at Csla.Silverlight.PropertyStatus.SetSource()
       at Csla.Silverlight.PropertyStatus.<.cctor>b__b(DependencyObject o, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
       at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
       at System.Windows.DependencyObject.RefreshEx pres sion(DependencyProperty dp)
       at System.Windows.Data.BindingExpression.SendDataToTarget()
       at System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener sender, PropertyPathChangedEventArgs args)
       at System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep source)
       at System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source)
       at System.Windows.DependencyPropertyListener.SourcePropertyChanged(DependencyObject sender, DependencyProperty dp)
       at System.Windows.Data.WeakDependencyPropertyChangedListener.SourcePropertyChanged(DependencyObject sender, DependencyProperty dp)
       at System.Windows.DPChangedEventHandler.Invoke(DependencyObject sender, DependencyProperty dp)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyProperty dp)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyProperty dp)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
       at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at Csla.Silverlight.ViewModelBase`1.set_Model(T value)
       at XXXXXX.XXXX.XXX.Silverlight.UI.ViewModels.Site.LocationGroupViewModel.<.ctor>b__1(Object o2, DataPortalResult`1 e2)
       at Csla.DataPortal`1.OnCreateCompleted(DataPortalResult`1 e)
       at Csla.DataPortal`1.proxy_CreateCompleted(Object sender, DataPortalResult`1 e)
       at Csla.DataPortalClient.WcfProxy`1.OnCreateCompleted(DataPortalResult`1 e)
       at Csla.DataPortalClient.WcfProxy`1.proxy_CreateCompleted(Object sender, CreateCompletedEventArgs e)
  InnerException:

I've done a bit of tracing and it appears to trigger as soon as the model is changed out by the BXF's InitializeBindingResource(IView view) method when it sets the:

I've done a bit of tracing and it appears to trigger as soon as the model is changed out by the BXF's InitializeBindingResource(IView view) method when it sets the:

...
var viewsource = resource as System.Windows.Data.CollectionViewSource;
                if ( viewsource != null )
                {
                    // make sure model is a list (or wrapped in one)
                    var list = view.Model as System.Collections.IEnumerable;
                    if ( list == null )
                        list = new List<object>() { view.Model };
                    viewsource.Source = list;
                }
                else
...


Not sure what the root cause is though, but it dissapears when I don't have the PropertyStatus control connected.

Regards,
Johann

 

ajj3085 replied on Monday, September 27, 2010

I'm not sure, but i think your binding is off.  The propert status should inherit its datacontext from its container, and then your binding should just be {Binding Path=Description}.  I'm not sure that dot notation will work.

Jaans replied on Monday, September 27, 2010

Woa.. PEBCAC! (Problem Exists Between Chair and Computer) Embarrassed

You hit the nail on the head. Since I switched to the MVVM pattern, the View now binds to a ViewModel and in the case of CSLA ViewModel, the ViewModel has a "Model" property that is a reference to the CSLA Business Object.

Often it's useful to add additional properties to the ViewModel that is practical to the View, but inappropriate for the business object "Model" (because it relates to the UI only), so generally the DataContext is set to the ViewModel and then binding is done to either the "custom property on the ViewModel", or "via the Model property to the business object" (E.g. Model.Description).

The "Property" property of the PropertyStatus control as you pointed out should be pointing directly at the Property, which is relative to the DataContext of the PropertyStatus. This doesn't support the Model notation, and now it's more clear to me in that the "DataContext" is supposed to set to the Business Object and the "Property" to be set to the individual property on said Business Object.

Thanks Andy. Big Smile

Copyright (c) Marimer LLC