Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Converter throwing Object reference not set at design time

I'm having an error at design time in my xaml:

Object not set to an instance of an object.

It's only happening at design time and runs perfectly in run time. The error is coming from my converter, the code which I have below. I thought it might be due to not checking if value was null or I had it returning null if it was null, but I changed both and it has made no difference.

And ideas are appreciated. Thanks

public class CouponDataSplitterConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,     CultureInfo culture)
    {
        if (value != null)
        {
            List<SelectionArea> selectionAreasList =
                new List<SelectionArea>((ObservableCollection<SelectionArea>) value);
            foreach (var area in selectionAreasList)
            {
                if (area.AreaName.Contains(parameter.ToString()))
                {
                    return area.SelectionRows;
                }
            }

            return selectionAreasList;
        }
        return "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

ItemsSource="{Binding Coupon.SelectionAreas, 
        ConverterParameter='Test Parameter',
        Converter={StaticResource CouponDataSplitterConverter}

Stack Trace:

at App.UI.Converters.CouponDataSplitterConverter.Convert(Object      value, Type targetType, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp)
at System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d, DependencyProperty dp)
at System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
at System.Windows.StyleHelper.GetChildValueHelper(UncommonField`1 dataField, ItemStructList`1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
at System.Windows.StyleHelper.GetChildValue(UncommonField`1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject  container, Int32 childIndex, FrameworkObject child, DependencyProperty dp,  FrugalStructList`1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry)
at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot)
at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot)
at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject)
at System.Windows.FrameworkTemplate.HandleBeforeProperties(Object createdObject, DependencyObject& rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope)
at System.Windows.FrameworkTemplate.<>c__DisplayClass0.<LoadOptimizedTemplateContent>b__5(Object sender, XamlObjectEventArgs args)
at System.Xaml.XamlObjectWriter.OnBeforeProperties(Object value)
at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
at System.Windows.FrameworkElement.ApplyTemplate()
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
like image 271
Spitfire5793 Avatar asked Sep 26 '22 23:09

Spitfire5793


1 Answers

If you're getting a NullReferenceException then something is null.

Your code checks explicitly for the value argument being null. However, you have this line of code in your foreach:

if (area.AreaName.Contains(parameter.ToString()))

This line could throw a NullReferenceException if either the AreaName property has a null value (the Contains method call would throw), or the parameter argument supplied to the method was null (the ToString method call would throw).

It might be worth putting some debugging code in your method, so you can check the status of the variables you are using. I know you can't use the debugger as this is a design-time problem, so you may have to log to a temporary text file or something like that.

like image 183
Steven Rands Avatar answered Sep 29 '22 08:09

Steven Rands