3

I use this code pattern all over and only in this particular property an ArgumentNullException is thrown when trying to assign a value to it (ie invoking the setter). The object being null is the isLoggedInLock,so it seems that it has not been instantiated before the setter is called. Wat is wrong??

EDIT: I commented out the OnPropertyChanged method to avoid confusion. The exception is still thrown. One other thing I can think of is that the update is done in another thread than the UI thread.

EDIT2: I instantiated the isLoggedInLock in the non-static constructor and still the exception is thrown. Something very strange is going on. I will investigate further.

FINAL EDIT: My apologies: I left out the key to the solution: the [DataMember] Attribute. The object comes into life by being deserialized. This skips the default constructor and the static initialisation of the isLoggedInLock. By adorning the isLoggedInLock with the [DataMember] attribute or by instantiating it in a method adorned with the [OnDeserialzed] attribute the problem goes away!

Thanks a bunch and the first to update his answer will get the points! ;-)

    [DataMember]
    private bool isLoggedIn;
    private readonly object isLoggedInLock=new object();
    public bool IsLoggedIn
    {
        get
        {
            lock (isLoggedInLock)
            {
                return isLoggedIn; 
            }
        }
        set
        {
            lock (isLoggedInLock)
            {
                isLoggedIn = value;
                //OnPropertyChanged("IsLoggedIn");
            }
        }
    }
4
  • Is there any code in the OnPropertyChanged method? Feb 14, 2010 at 13:59
  • 1
    Please catch the exception and post the results of ex.ToString() Feb 14, 2010 at 14:35
  • And is this an ArgumentNullException or a NullReferenceException like you have tagged? Feb 14, 2010 at 14:49
  • ArgumentNullException, the tag is inaccurate
    – Dabblernl
    Feb 14, 2010 at 15:05

5 Answers 5

4

The only reason an ArgumentNullException can be thrown in the code you posted is if isLoggedInLock is null when you execute the lock statement. So unless you are explicitly setting isLoggedInLock to null elsewhere (and this can only be in the constructor since the field is readonly), your assumption is correct:

it seems that it has not been instantiated before the setter is called

Fields will be initialized in the order they are declared, so if you have a field initialization higher up that calls a member that accesses the IsLoggedIn property, this will happen before isLoggedInLock is initialized.

If you look at the stack trace from the exception it should be easy to work out what's going on.

3

The most likely cause, from a 10 second look at the code, is OnPropertyChanged.

I am guessing from the naming that it raises an event. Can we see the code for this method? Chances are it is invoking the event handler without first doing a null check to check someone is subscribed.

[Edit - sorry didn't read that you'd identified isLoggedInLock as the culprit]

Are you sure it is isLoggedInLock - as there doesn't look like much scope for this being null. As it is marked as readonly, it can't possibly be being set to null in elsewhere in the method.

Is this an accurate representation of the affected code? Definitely not the case that the property is static, or some such difference?

0
2

Do you have a static constructor? (and does it use IsLoggedIn ?)

2

It is possible that a field of a class is null, even when it is initialized in the constructor: when the class comes into life through deserialization, the constructor is not called....

0

Is posible, the reflection was set the "isLoggedInLock" to the null? With FieldInfo.SetValue() you can change value of readonly field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.