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.
To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.
The message "object reference not set to an instance of an object" means that you are referring to an object the does not exist or was deleted or cleaned up. It's usually better to avoid a NullReferenceException than to handle it after it occurs.
There were several reasons why this error Object reference not set to an instance of an object comes are listed below: The null value of variable or argument. A null value for any activity from which you are getting the text from any website. Maybe you have created duplicate variables. Import arguments are not updated.
We can Create objects in C# in the following ways: 1) Using the 'new' operator: A class is a reference type and at the run time, any object of the reference type is assigned a null value unless it is declared using the new operator.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With