Jay Jay - 7 months ago 26
Vb.net Question

WPF Designer Binding with Converter throwing Object Reference not set to instance of an object

My XAML designer shows Object reference not set to instance errors everywhere I'm using one of my converter while binding. After digging a lot I've found that it happens at this particular line because it is not able to find get the value at design time.

TransactionViewModel.getInstance()


Even when i try to avoid it by checking it is not null, it still throws the object reference error

TransactionViewModel is a singleton class with 'getInstance()' function to get the instance.

This is my convert function as follows

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Dim FieldName As String = value.ToString
Dim FieldNum As Integer = Integer.Parse(parameter)

If TransactionViewModel.getInstance IsNot Nothing Then
If TransactionViewModel.getInstance._NR IsNot Nothing Then
If TransactionViewModel.getInstance._NR.ContainsField(FieldNum, FieldName) Then
Return Visibility.Visible
Else
Return Visibility.Hidden
End If
End If
End If
Return Visibility.Visible
End Function


Any suggestions?

Answer

Please try with this it may help you

if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
    //Code that throws the exception
   //Code that return null
}

VB:

If Not System.ComponentModel.DesignerProperties.GetIsInDesignMode(New DependencyObject) Then
        'Code that throws the ex
        'Code that return null
End If
Comments