This project has moved and is read-only. For the latest updates, please go here.

EventToCommand vs. NullReferenceException

Mar 24, 2010 at 6:29 PM


Hi Laurent (+ community),

Firstly, for someone with heavy deadlines, "old gen" tools (VS2008, Blend 3), and little patience, your [Laurent] talk at MIX10 was awesome in helping me get to grips with MVVM in terms of concepts and implementation without requiring a whole bunch of training/components/dependencies.

Now, my problem ...

I love EventToCommand and want to use it in my VS2008/WPF3.5SP1 project but am getting a NullReferenceException when I try and use it.

With my XAML:


     xmlns:telerik="" xmlns:Telerik_Windows_Controls_RibbonBar_Primitives="clr-namespace:Telerik.Windows.Controls.RibbonBar.Primitives;assembly=Telerik.Windows.Controls.RibbonBar" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras" x:Class="xxx.Windows.Presentation.Filing.View.PrefaceMessageForClientAndContactWindow"
    Title="Create Message"
     Height="528" Width="700" MinHeight="450" ShowInTaskbar="False" WindowStartupLocation="CenterOwner">
            <viewmodel:PrefaceMessageForClientAndContactViewModelLocator x:Key="ViewModelLocator" d:IsDataSource="True"/>
        <Binding Mode="OneWay" Path="Main" Source="{StaticResource ViewModelLocator}" />
			<RowDefinition Height="Auto" />
			<RowDefinition Height="*" />
			<RowDefinition Height="Auto" />

		<!-- snip to the action -->

								<telerik:RadTreeView  MinHeight="150" Margin="{DynamicResource TextBoxMargin}" IsLoadOnDemandEnabled="True" ItemsSource="{Binding ClientSearchResults}" ItemTemplate="{StaticResource clientsHierarchicalDataTemplate}">
										<i:EventTrigger EventName="LoadOnDemand">
											<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ContactsExpansionCommand, Mode=Default}"/>

You'll see I am using a RadTreeView, but this issue occurs on any control from basic TextBoxes up.

My [reduced] ViewModel:


	public class PrefaceMessageForClientAndContactWindowViewModel : ViewModelBase
		#region ~ Properties ~


		public RelayCommand ContactsExpansionCommand { get; private set; }

		public IClientService ClientService { get; private set; }


		#region ~ Constructors ~

		public PrefaceMessageForClientAndContactWindowViewModel(IClientService clientService)
			ClientService = clientService;
			ContactsExpansionCommand = new RelayCommand(
				() =>




		private void ExpandContacts()
			int i = 5; // I put a breakpoint here for debugging purposes. (Never hits, as exception is thrown)





Whenever I run this, I always get a stack trace thus:


System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
       at System.Windows.Markup.ReflectionHelper.IsPublicType(Type type)
       at System.Windows.Markup.XamlTypeMapper.UpdateAttachedPropertyMethdodInfo(BamlAttributeInfoRecord attributeInfo, Boolean isSetter)
       at System.Windows.Markup.XamlTypeMapper.UpdateAttachedPropertyGetter(BamlAttributeInfoRecord attributeInfo)
       at System.Windows.Markup.PropertyDefinition.get_AttachedPropertyGetter()
       at System.Windows.Markup.BamlCollectionHolder.InitDefaultValue()
       at System.Windows.Markup.BamlCollectionHolder..ctor(BamlRecordReader reader, Object parent, Int16 attributeId, Boolean needDefault)
       at System.Windows.Markup.BamlRecordReader.ReadPropertyIListStartRecord(BamlPropertyIListStartRecord bamlPropertyIListStartRecord)
       at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
       at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)
       at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()
       at System.Windows.Markup.TreeBuilder.Parse()
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at xxx.Windows.Presentation.Filing.View.PrefaceMessageForClientAndContactWindow.InitializeComponent() in d:\dev2008\xxx\xxx.Windows.Presentation\Filing\View\PrefaceMessageForClientAndContactWindow.xaml:line 1
       at xxx.Windows.Presentation.Filing.View.PrefaceMessageForClientAndContactWindow..ctor() in D:\dev2008\xxx\xxx.Windows.Presentation\Filing\View\PrefaceMessageForClientAndContactWindow.xaml.cs:line 25
       at xxx.Windows.MSOffice.Presentation.DialogInvoker.CreateMessageFromxxxClientAndContact() in D:\dev2008\xxx\xxx.Windows.MSOffice\Presentation\DialogInvoker.cs:line 18
       at xxx.Windows.MSOffice.Client.AddinModule.adxCommandBarNewMessageForContactButton_Click(Object sender) in D:\dev2008\xxx\xxx.Windows.MSOffice.Client\AddInModule.code.cs:line 48
       at AddinExpress.MSO.ADXCommandBarButton.OnButtonClick(ICommandBarButton button, Boolean& handled)

It appears to fail on the InitializeComponent() call within the PrefaceMessageForClientAndContactWindow constructor.

Is there anything I am missing as to why I should get this exception so consistently? I really don't want to have had to take the time to learn MVVM and then have to frig it to get it to work.


Thanks for your help.



Mar 24, 2010 at 9:13 PM

I cannot see anything wrong on first sight, it should work.

If you can, try to create a repro of the issue and send it my way please.




Mar 25, 2010 at 5:30 PM

Hi Laurent,

I did try to create a mock-up for you and sod's law, it works fine. I've tried mimicking as much of my [complex] project as I can but cannot get it to reproduce the issue! All I know is that as soon as I removbe the:



										<i:EventTrigger EventName="LoadOnDemand">
											<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ContactsExpansionCommand, Mode=Default}"/>



from the XAML, it works fine - but I lose my cool eventing. Unless I remove the entire of that block, it continues to throw an exception on the InitialiseComponent call. I cannot step inside the call, and have even tried removing the NonUserCode attribute from the generated cde (which seems to persist between compiles) but no luck.


Mar 28, 2010 at 7:55 PM

Hey programx,

I could be wrong, but it looks like you are missing a reference to the GalaSoft_MvvmLight_Command namespace.  Shouldn't the following line be syntax highlighted?

<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ContactsExpansionCommand, Mode=Default}"/>

Mar 29, 2010 at 6:52 AM


Thanks for your reply.

I can't explain the lack of formatting, maybe it was how I pasted it in. The namespace is:


(This can be seen at the extreme right of the first post)

I assume this is correct? It does compile, though.

Apr 18, 2010 at 9:53 PM


The GalaSoft_MvvmLight_Command is OK. It is the way that Blend creates new xmlns statements. You should see a corresponding xmlns:GalaSoft_MvvmLight_Command in the UserControl tag.