-2

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);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rod
  • 4,107
  • 12
  • 57
  • 81

1 Answers1

0

This issue most likely is doing what you believe it is doing, instantiating a viewmodel in xaml (and not code behind like the 2nd example). The exception is most likely coming from something on the viewmodel throwing an exception.

Run the code in debug mode and see where the breakpoint hits to determine how to resolve.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Finally got an answer. Talked with a coworker (tried to yesterday, but he wasn't available). He suggested I move the declaration of the modal view back to in a resource dictionary I'd moved it out of. ``` ``` I moved this to the parent XAML's resources, but it didn't work there. I'd like to know why putting it in a global resource dictionary worked, but didn't work in the parent view's XAML? – Rod Feb 25 '20 at 18:21
  • 1
    Create a task example that reproduces the problem and then re ask the question. If I would bet it's probably a issue in relation to the viewmodel and what it contains and not the location of where it's defined – ΩmegaMan Feb 25 '20 at 18:27