Bug 37088 - Null reference exception when you set a binding on a bindable property with a validate callback
Summary: Null reference exception when you set a binding on a bindable property with a...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-12-16 16:04 UTC by gmurray
Modified: 2017-01-18 13:19 UTC (History)
5 users (show)

Tags: ac bindings
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description gmurray 2015-12-16 16:04:17 UTC
If you use the generic create method for a bindable property and specify a validate value callback, and set a binding on that property, XF throws a NullReferenceException with this stacktrace:
   at Xamarin.Forms.BindableProperty.<>c__DisplayClass13_0`2.<Create>b__0(BindableObject bindable, Object value)
   at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
   at Xamarin.Forms.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget)
   at Xamarin.Forms.BindingExpression.Apply(Object sourceObject, BindableObject target, BindableProperty property)
   at Xamarin.Forms.Binding.Apply(Object newContext, BindableObject bindObj, BindableProperty targetProperty)
   at Xamarin.Forms.BindableObject.SetBinding(BindableProperty targetProperty, BindingBase binding, Boolean fromStyle)
   at Xamarin.Forms.BindableObject.SetBinding(BindableProperty targetProperty, BindingBase binding)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, BindableObject rootElement, INode node, HydratationContext context, IXmlLineInfo lineInfo)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit(ElementNode node, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.<>c__DisplayClass26_0.<SetDataTemplate>b__0()
   at Xamarin.Forms.DataTemplate.CreateContent()
   at Infragistics.XF.Controls.WP.TemplatedCell.UpdateContent(DataTemplate cellTemplate, Object data)
   at Infragistics.XF.Controls.WP.TemplatedColumnDefinition.GetCell(IGGridView gridView, IGListAdapter adapter, IGCellPath path)
   at Infragistics.Controls.Grid.Columns.IGColumnDefinition.OnRetrieveCell(IGridView gridView, BaseAdapter adapter, IGCellPath path)
   at Infragistics.Controls.Grid.Definitions.BaseDefinition.RetrieveCell(IGridView gridView, BaseAdapter adapter, IGCellPath path)
   at Infragistics.Controls.Grid.Adapters.BaseAdapter.RetrieveCell(IGridView gridView, IGCellPath cellPath)
   at Infragistics.Controls.Grid.GridController.GetCell(CellModel model)
   at Infragistics.Controls.Grid.Layout.CellLayoutPanel.DoRefresh()
   at Infragistics.Controls.Grid.Layout.CellLayoutPanel.ScheduleRefresh(Boolean immediate)
   at Infragistics.Controls.Grid.Layout.CellLayoutPanel.Refresh()
   at Infragistics.Controls.Grid.GridController.SizeChanged(Double width, Double height)
   at Infragistics.Controls.Grid.IGGridView.IGGridView_SizeChanged(Object sender, SizeChangedEventArgs e)
   at System.Windows.FrameworkElement.OnSizeChanged(Object sender, SizeChangedEventArgs e)
   at MS.Internal.JoltHelper.RaiseEvent(IntPtr target, UInt32 eventId, IntPtr coreEventArgs, UInt32 eventArgsTypeIndex)

This creation overload results in an exception being thrown when the binding is set:
BindableProperty.Create<SomeComponent, double>(p => p.Value, 0, BindingMode.OneWay, (o, v) => { ((SomeComponent)o).MarkHasValue("Value"); return true; });

But this one is ok:
BindableProperty.Create("Value", typeof(double), typeof(SomeComponent), 0.0d, BindingMode.OneWay, (o, v) => { ((SomeComponent)o).MarkHasValue("Name");  return true; });
This was working in an earlier version of XF, so I believe it must be a regression. 
If you have any issues reproducing the issue, these may be salient aspects of the scenario:
* The BindableProperty is a primitive type (which means bad stuff would happen if you tried to cast a null object to it)
* The binding being set probably initially has a null source (to be provided later) rather than the source being initially valid.

Would appreciate quick action on this, if possible, o/w we'll have to reorg all of our BindableProperties and eat quite a bit of risk doing so.
Comment 1 Paul DiPietro [MSFT] 2015-12-16 16:54:51 UTC
If you wouldn't mind putting a reproduction together it would be much appreciated.
Comment 2 Stephane Delcroix 2016-01-05 15:13:41 UTC
I was able to reproduce and confirm this issue.
Comment 3 Stephane Delcroix 2017-01-18 13:19:57 UTC
Generic versions of `BindableProperty.Create()` are now deprecated.

As this is not a regression, I'm closing this issue