5

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()
Spitfire5793
  • 359
  • 1
  • 14
  • Do you have a stack trace that indicates anything useful? – Jon Skeet Dec 14 '15 at 10:42
  • 2
    (It's also not clear why you're creating a new `List` instead of just iterating over the existing collection...) – Jon Skeet Dec 14 '15 at 10:43
  • I've added the stack trace. That is also a good point, I am not sure why I decided to use a list but surely that wouldn't make any difference would it? – Spitfire5793 Dec 14 '15 at 11:01
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Dec 14 '15 at 11:09
  • You don't appear to have the `ConvertBack` method in your class, or has that been omitted from the question? Also, the return type of your converter should be consistent: you are returning a generic list in one branch and a string in the other. Not saying either of these things would necessarily be the problem though. – Steven Rands Dec 14 '15 at 11:16
  • I do have convert back, just omitted from the question but I will add it in. As for the return string type, I originally had it as null but thought that could have been what was causing the problem. – Spitfire5793 Dec 14 '15 at 11:19
  • 1
    Do you have any line numbers? The more precisely we know where the exception is thrown, the better... – Jon Skeet Dec 14 '15 at 11:37
  • How are you binding the converter, are you sure ConverterParameter is not being passed as null at design time? – Ahmad Dec 14 '15 at 11:45
  • Line numbers on the converter? I've added the XAML for how the converter is used – Spitfire5793 Dec 14 '15 at 11:49
  • Line numbers for the stack trace, does it say in what line are you getting the exception? – Dzyann Dec 14 '15 at 12:05
  • It doesn't im afraid, is it possible to get line numbers for it? – Spitfire5793 Dec 14 '15 at 12:12
  • Sorry I had to edit it to make it less obvious what the the code was part of, just missed that bit – Spitfire5793 Dec 14 '15 at 12:41

2 Answers2

3

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.

Steven Rands
  • 4,379
  • 3
  • 16
  • 46
  • That did it, checking if the areaName and parameter were null got it working again. Seems so simple but I couldn't see it. Thanks a lot! – Spitfire5793 Dec 14 '15 at 15:29
  • +1 your point on `NullReferenceException` does help me. I'm not aware of it. I have handle the value to make sure if null, what it should return and proceed with variable validation. – Luiey Jul 22 '20 at 06:41
1

It is not easy to tell what is being set to null , value could be initially null if it was not initialized in the viewmodel constructor (MarkSenseCoupon in this case i assume). Have a look at Debugging Your Custom Control at Design Time for steps on how to debug your control at design time and simply put a break point at the start of the convert method to figure out what object is set to null.

This SO question also seem to deal with a similar issue WPF Converter casting causes Visual Studio designer exception ...

Community
  • 1
  • 1
Ahmad
  • 1,312
  • 11
  • 23