6

I'm implementing INotifyPropertyChanged in a base class as follows:

public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void RaisePropertyChanged(string propertyName)
    {
        var propChangedHandler = PropertyChanged;

        if (propChangedHandler != null)
        {
            var args = new PropertyChangedEventArgs(propertyName);
            propChangedHandler(this, args);
        }
    }
}

I'm using it as follows:

RaisePropertyChanged("Name");

I'm getting a NullReferenceException while the arguments, "this" and the handler are NOT null. Can anyone shed some light on this?

Thanks.

-> Full stacktrace of the exception: http://pastebin.com/bH9FeurJ

UPDATE The exception occurs when I overwrite an instance of the class which contains this property. Simplified example:

public class Person : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }

// More properties etc.
}

-snip-

public class ViewModel
{
    private Person _dummyPerson;
    public Person DummyPerson
    {
        get { return _dummyPerson; }
        set
        {
            _dummyPerson = value;
            RaisePropertyChanged("DummyPerson");
        }
    }

    public void Foo()
    {
        DummyPerson = new DummyPerson(); 
        // this line throws the NRE, strangly enough the very FIRST time it works fine
    }
}

-snip-

I'm using this DummyPerson and its Name property to databind to the UI. The second and all following attempts thereafter result in the NullReferenceException.

rumblefx0
  • 665
  • 1
  • 9
  • 22

3 Answers3

3

The exception isn't raised in your sample code, it is raised in one of the subscribed event handlers. Go through it step by step in debugger or turn on the switch "Thrown" for "Common Language Runtime Exceptions" in "Debug" - "Exceptions" menu of Visual Studio. Then you will be able to find out the reason.

Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • I cannot step into the code any deeper, it automatically steps over. The switch you mention is also on and throws the error at the above-mentioned line. – rumblefx0 Dec 12 '11 at 15:37
1

I've had this error for a while, however, I have now solved it (although it may be a different cause within my code) - I was (rather foolishly) not checking for null in one of my IValueConverter implementations, (and for some reason, the code would not allow me to step into this code) and causing an exception as null was passed in as the value.

Dave Roberts
  • 180
  • 1
  • 1
  • 8
1

Looking at the stack-trace, it's clear that the NullReferenceException isn't being thrown here at all; it's actually being thrown deeper, at:

 GalaSoft.MvvmLight.Command.RelayCommand`1.Execute(Object parameter)

Essentially, a dependency of one of the event-listeners is misbehaving.

Aside: I wasn't quite sure if the stack-trace convention you are using is the 'grow-up' or 'grow-down' kind. Once I saw that the method just above yoir method was the delegate invocation:

System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)

...it was clear that the stack was indeed growing 'up'.

Ani
  • 111,048
  • 26
  • 262
  • 307
  • Yes, the first (deepest) step is at the top, while the final step before the exception is at the bottom; from VS2010. – rumblefx0 Dec 12 '11 at 15:35