tags:

views:

58

answers:

1

Hi,

I have multiple viewModels in my application and am binding/used them in ViewModelLocator mvvm light. I have done button on one of my DailyActivities.xaml page. but when i clicked on it,it gives me error in ViewModelLocator like "Object reference not set to an instance of an object". and control comes to this line :

public static void ClearActivities() { _activities.Cleanup(); //Error here _activities = null; }

here is the code for DailyActivitiesViewModel in ViewModelLocator:

private static ActivitiesViewModel _activities;

          public static ActivitiesViewModel ActivitiesStatic
    {
        get
        {
            if (_activities == null)
            {
                CreateActivities();
            }

            return _activities;
        }
    }

    /// <summary>
    /// Gets the ViewModelPropertyName property.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
        "CA1822:MarkMembersAsStatic",
        Justification = "This non-static member is needed for data binding purposes.")]
    public ActivitiesViewModel Activities
    {
        get
        {
            return ActivitiesStatic;
        }
    }

    /// <summary>
    /// Provides a deterministic way to delete the ViewModelPropertyName property.
    /// </summary>
    public static void ClearActivities()
    {
        _activities.Cleanup(); --Error here
        _activities = null;
    }

    /// <summary>
    /// Provides a deterministic way to create the ViewModelPropertyName property.
    /// </summary>
    public static void CreateActivities()
    {
        if (_activities == null)
        {
            _activities = new ActivitiesViewModel();
        }
    }

Kindly Suggest?

Thanks

+1  A: 

You need to determine if the field exists before you can use it: It seems like if the field _activities is already null then no clean-up is required.

why not do something like

public static void ClearActivites()
{
    if (null == _activities) return;
    _activities.Cleanup();
    _activities = null;
}
IanR
@IanR. Thanx the above helped me a lot. But now I have a dialog box with OK and Cancel button on one window. When i come to this window from other window and if I click on this button it asks for OK or Cancel to click. when i click on cancel, window gets closed but if i again repeat the process and want to cancel the window i have to click the cancel button twice and again n again means thrice four n so on. Kindly Suggest?Thanks
Tarun
How to check for the null for this dialog box?Kindly Suggest?
Tarun
@Tarun: Share some code for your question in the comment. Else consider asking a new question.
Veer
@Veer. That is done now by Using the Messenger.Reset().Thanks
Tarun
@Tarun: If this answer helped you, you can mark this as answer.
Veer
@Veer thanks - I was thinking that, but didn't want to see like I was just after the rep ;)
IanR