Create Sample Data from Class in Blend throws "Object reference not set to an instance of an object "

947 Views Asked by At

I am trying to create some Sample Data from my ViewModel classes in Expression Blend. However Expression Blend stops and says "Object reference not set to an instance of an object". Personally, I don't understand where this exception comes from.

Does anyone have an idea of why this is happening?

This is my UsersListViewModel:

[Export]
public class UserListViewModel : ViewModelBase
{
    [ImportingConstructor]
    public UserListViewModel(IUserListView view)
        : base(view)
    {

    }

    private ObservableCollection<UserItem> _userList;

    public ObservableCollection<UserItem> UserList
    {
        get { return _userList; }
        set
        {
            if (_userList != value)
            {
                _userList = value;
                RaisePropertyChanged("UserList");
            }
        }
    }

    private UserItem _selectedUser;

    public UserItem SelectedUser
    {
        get { return _selectedUser; }
        set
        {
            if (_selectedUser != value)
            {
                _selectedUser = value;
                RaisePropertyChanged("SelectedUser");
            }
        }
    }

    private string _searchText;

    public string SearchText
    {
        get { return _searchText; }
        set
        {
            if (_searchText != value)
            {
                _searchText = value;
                RaisePropertyChanged("SearchText");
            }
        }
    }

    private ICommand _searchCommand;

    public ICommand SearchCommand
    {
        get { return _searchCommand; }
        set
        {
            if (_searchCommand != value)
                _searchCommand = value;
        }
    }

    // ... other ICommands
}

Thank you in advance for all your help,

Cheers, G.

2

There are 2 best solutions below

0
On

When faced with this problem, I found that the Attributes on my properties cause this error message.

Commenting out [ImportingConstructor] and [Export] while creating the sample data (compile the project once with Blend to be sure not to work with the old version) might do the trick here.

2
On

UPDATE! Laurent (MvvmLight author) has posted how to debug design time data. Blog post here.

I found the cause and solution to this error in Blend or when opening a .xaml in Visual Studio.

Object reference not set to an instance of an object.

Blend attempts to run your design time code and it if hits a null pointer somewhere, this is the error you get.

So, track through your code creating the design time data. Most likely you forgot to initialize something or maybe you have the wrong type.

This would be easy to find if you could have breakpoints catch when the designer is running user code, but I don't think this is possible.