I'm trying to follow the example on this page of connecting the datacontext of a user control, to the viewmodel that's to provide the data. However, when I do, then build the project I get an "Object reference not set to an instance of an object" error. I don't know what I'm missing here. Here's the declaration in the header of my usercontrol:
<UserControl
x:Class="PharmacyWarehouse.View.ProgramSelectView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
xmlns:convert="clr-namespace:PharmacyWarehouse.View.ValueConverters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:PharmacyWarehouse.View"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:PharmacyWarehouse.ViewModel"
Width="500"
Height="420"
Background="Orange"
mc:Ignorable="d">
<UserControl.DataContext>
<vm:ProgramSelectViewModel />
</UserControl.DataContext>
It's complaining about the <vm:ProgramSelectViewModel />
.
The reason why I don't think this is a duplicate to other questions and answers about nullreference, is because other views and viewmodels are similarly written and do not raise nullreference.
The ProgramSelectViewModel has over 500 lines in it, so I won't reproduce it here. I'll provide the constructors:
[PreferredConstructor]
public ProgramSelectViewModel() : this(false) { }
//-----------------------------------------------------------------------------------------
// ProgramSelectViewModel
//-----------------------------------------------------------------------------------------
public ProgramSelectViewModel(bool unit_testing)
{
Debug.WriteLine($"ProgramSelectViewModel: In constructor; this == {this.ToString()}");
var connString = MainDataContext.Database.Connection.ConnectionString;
var userRoles = new DatabaseRolesAndPermissions(connString);
perms = userRoles.TablePermissions("PW.Program");
SaveProgramSelectCommand = new RelayCommand(ExecuteSaveProgramSelectCommand, CanSaveProgramSelectCommand);
CancelProgramSelectCommand = new RelayCommand(ExecuteCancelProgramSelectCommand);
Messenger.Default.Register<ProceedWithProgramSelectUpdateMessage>(this, UpdateProgramSelect);
Messenger.Default.Register<OpenProgramSelectMessage>(this, ReadMessageData);
Messenger.Default.Register<ParentObjectMessage>(this, ReceiveParentObjectMessage);
}