0

I have a WPF Project and I am using a RelayCommand for button click event. Here is the constractor of my MainViewModel

    private readonly DataService _dataService;
      public MainWindowModel(DataService dataService)
    {
        _dataService = dataService;
    }

// RelayCommandClass

    public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;
    private Action<object> action;

    #endregion // Fields

    #region Constructors


    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    //public RelayCommand(Action<object> action)
    //{

    //    this.action = action;
    //}
    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}

//Region for Command Implementation

    private RelayCommand _validateResponse;

    public RelayCommand ValidateResponse
    {
        get
        {
            return _validateResponse ?? (_validateResponse = new RelayCommand(
                                                                 parm => _dataService.Validate("string1","string2"))
                     );


        }

    }

But When I run the project I keep getting a null reference exception. Did I miss something? Thanks

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
HXD
  • 506
  • 1
  • 7
  • 26
  • at which line you are getting null reference exception.And this exception comes when your object is not initialized so put break point check which object is null.hope it help :) – loop May 16 '14 at 15:53
  • When does then exception is being thrown? on command execute? on app start? add it's stack trace – Jossef Harush Kadouri May 16 '14 at 16:00
  • @loop the exception is at _dataService.Validate("string1","string2")) The _dataService shows null but Inject the DataService Class to the MainWindowModel() contractor. – HXD May 16 '14 at 16:01
  • 2
    @HXD check when you creating the instance of 'MainWindowModel'. you might be passing a null value to 'dataService' – Jossef Harush Kadouri May 16 '14 at 16:05
  • The first thing you're missing is the exception. And I don't mean a picture of the exception dialog. On the dialog you'll see a link for copying details to the clipboard. Click that, then [edit] and add them. –  May 16 '14 at 17:11
  • @JossefHarush that was exactly what the problem was thanks. – HXD May 16 '14 at 18:39

1 Answers1

1

Initializing the _dataService object at the constructor(constructor two) used by xaml solved the problem

contractor one:

  public MainWindowModel(DataService dataService)
    {
        _dataService = dataService;
    }

contractor two:

 public MainWindowModel()
    {
         _dataService = new DataService()
    }
HXD
  • 506
  • 1
  • 7
  • 26