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

2

7

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.

Wallaroo answered 14/2, 2011 at 11:26 Comment(1)
Did you find a solution for this problem?Alec
D
2

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.

Dicta answered 9/7, 2013 at 18:23 Comment(2)
unfortunately the constructor of the View associated to the viewModel doesnt have any code at all. So, I don't think it can be the cause of the problem.Wallaroo
Debug the problem! Link put in the answer.Dicta
A
1

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.

Alec answered 15/8, 2011 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.