How to Fix null reference in value Converter in WPF?

MERUN KUMAR MAITY 636 Reputation points
2022-01-22T19:02:08.723+00:00

I have a value converter which I mainly use for data binding of the width property for various controls. But If I use the same converter to bind of Height then I get a variety of error. And I have another problem is, I get null reference error.

Here is the C# code of the Converter which I use for data binding of Width :

public class WpConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Int32.Parse(value.ToString()) - 200;
        }

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


    }

This same converter is not working for Height, I don't understand why ? because the main goal is to get a numerical value from the converter. But still my WPF application is crashed whenever I use for data binding of Height of any elements. And another null reference error I get on this line (value.ToString()).

This converter simply return one time value that means we only get one value at a time. But I want to use the converter multiple times that's why I use a Converter Parameter. Here is the C# code of that same converter but slightly modified to use for Converter Parameter.

public class RectConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double disp;
            if (parameter == null || !double.TryParse(parameter.ToString(), out disp)) disp = 142;
            return double.Parse(value.ToString()) - disp;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
Developer technologies Windows Presentation Foundation
Developer technologies C#
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-01-30T04:48:41.993+00:00

    Hi,
    at first time the converter is called up, the DockPanel is not yet set up. You have to intercept that, e.g. like this:

      public class RectConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          double disp;
          if (parameter == null || !double.TryParse(parameter.ToString(), out disp)) disp = 142;
          double val;
          if (value == null || !double.TryParse(value.ToString(), out val)) val = 142;
          return (val < disp) ? 1.0 : val - disp;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
          => throw new NotImplementedException();
      }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-01-22T20:46:01.05+00:00

    You can implement IDataErrorInfo interface, in the indexer perform validation and present an error message to the user.

    Or perhaps

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is null ? 0 : int.TryParse(value.ToString(), out var valueResult) ? valueResult - 200 : 0;
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.