NullPointerException Raised in SimpleContainer

Mar 27, 2011 at 2:26 AM

Hi, I'm currently working on a small Windows Phone 7 application, using Caliburn Micro.

But I've encountered a problem, when I launch my application in the debugger, I get a NullpointerException originating from the SimpleContainer class, in

protected virtual object ActivateInstance(Type type, object

[] args)

 

 

System.NullReferenceException was unhandled
  Message=NullReferenceException
  StackTrace:
       at System.Activator.InternalCreateInstance(Type type, BindingFlags invokeAttr, Binder binder, Object[] args, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type, Object[] args)
       at Itdagarna.Framework.SimpleContainer.ActivateInstance(Type type, Object[] args)
       at Itdagarna.Framework.PhoneContainer.ActivateInstance(Type type, Object[] args)
       at Itdagarna.Framework.SimpleContainer.BuildInstance(Type type)
       at Itdagarna.Framework.SimpleContainer.<>c__DisplayClass4.<RegisterPerRequest>b__3()
       at Itdagarna.Framework.SimpleContainer.GetInstance(Type service, String key)
       at Itdagarna.AppBootstrapper.GetInstance(Type service, String key)
       at Itdagarna.Framework.ViewModelLocator.<.cctor>b__0(Type viewType)
       at Itdagarna.Framework.ViewModelLocator.<.cctor>b__1(Object view)
       at Itdagarna.Framework.FrameAdapter.OnNavigated(Object sender, NavigationEventArgs e)
       at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, PhoneApplicationPage existingContentPage, PhoneApplicationPage newContentPage)
       at System.Windows.Navigation.NavigationService.CompleteNavigation(DependencyObject content)
       at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
       at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
       at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
       at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at System.Delegate.DynamicInvokeOne(Object[] args)
       at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
       at System.Delegate.DynamicInvoke(Object[] args)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
       at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
       at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
       at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
       at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

I suspect there's something wrong with my Bootstrapper, but I haven't changed much from the DateBinding, since I'm new to Caliburn - does anyone have a good idea on how to debug the problem?

Thanks!

 
Mar 27, 2011 at 2:29 AM

With DataBinding, I refer to the example made available at http://joshearl.wordpress.com/2011/03/20/databinding-wp7-lists-to-singletons-with-caliburn-micro/

Coordinator
Mar 27, 2011 at 10:23 AM

Make sure that all types you need to resolved from the container, are registered therein. For WP7 the ViewModelLocator requires the you register view models for pages using a string key.

Mar 27, 2011 at 11:56 AM
Edited Mar 27, 2011 at 12:00 PM

Hi Rob,

Thank you for your quick reply, I really appreciate it!

Unfortunately, as far as I can tell it's correctly registered in my Bootstrapper, 

Content of AppBootstrapper.cs in Root

container.RegisterPerRequest(typeof(MainPageViewModel), "MainPageViewModel", typeof(MainPageViewModel));
...


I've uploaded my Solution to Skydrive here , I've tried to remove as much functionality in order to identify the problem, but to no avail. Would it be possible for you to take a quick look to see If I've missed anything in setting up the Caliburn.Micro for WP7?

Thank you!

* Seems to be some problem with Skydrive, here's the link in clear text http://cid-5b85cfa3aff2953a.office.live.com/embedicon.aspx/.Public/Itdagarna.zip

Coordinator
Mar 27, 2011 at 6:10 PM
Edited Mar 27, 2011 at 6:11 PM

Your problem is caused by the fact that you have referenced both Caliburn.Micro the compiled assembly and embedded the source code simultaneously. Then in your view model, you reference one version of the navigation service, but you register another. Therefore the dependencies cannot be properly resolved. As a side note, I saw that you were using two different ctors so that you can manually create multiple instances of the view model in Xaml. I don't advise that. There should be one instance of that view model for the entire page. Additionally, all instances created through xaml will not have their dependencies injected because the parameterless ctor is called by the xaml parser.

Mar 27, 2011 at 7:26 PM

Thank you Rob,

I have no excuse for this, I'm quite embarrased by the fact that I included both the uncompiled source code to the framework and the assembly. I think the reason was that SimpleContainer and PhoneContainer wasn't found in the assembly, when I tried out the databinding sample. Then It spun out of control as I tried to retrace my steps.

I've now also fixed other issues with the Solution, and removed the default constructor. Again, thank you for taking the time to look at it, it's now working perfectly and it has allowed me to resume developing the application.

I'm sorry if you feel offended by my brief laps of no-brain syndrome! :)

Coordinator
Mar 28, 2011 at 3:53 AM

I'm not easily offended :) I've had my share of moments too...

Mar 29, 2011 at 4:29 PM
Edited Mar 29, 2011 at 4:32 PM

I got another question concerning the Action Messages,

 

       <ListBox x:Name="Feedbox" ItemsSource="{Binding Feed}"  cal:Message.Attach="[Event SelectionChanged] = [Action Test(Title.Text, Link.Text)]">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <toolkit:WrapPanel Orientation="Horizontal">
                                    <StackPanel Margin="0,0,0,10">
                                    <TextBlock x:Name="Title" Text="{Binding Title}" 
                     FontSize="32" TextWrapping="Wrap" MaxWidth="320" Style="{StaticResource PhoneTextNormalStyle}"/>
                                        <TextBlock  Text="{Binding PublishedDate}" HorizontalAlignment="Left" FontWeight="SemiBold" FontSize="16" Width="320" MaxHeight="85" TextWrapping="Wrap"  Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}" FontStretch="ExtraExpanded" Foreground="{StaticResource PhoneAccentBrush}" />
                                    <TextBlock x:Name="ShortDescription" Text="{Binding ShortDescription}" HorizontalAlignment="Left"   FontSize="16" Width="320" MaxHeight="85" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                    <TextBlock x:Name="Link" Text="{Binding Link}" Visibility="Collapsed"/>
                                    </StackPanel>
					.....
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

I'm unsure about the use of cal:Message.Attach="[Event SelectionChanged] = [Action Test(.....)]" I've tried multiple combinations, with $datacontext, splitting it up into Title.Text and Link.Text, sending in SelectedItem or SelectedIndex etc etc... the "Test" function works, it displays a Messagebox, but everything that comes in is null, if I don't send in hardcoded strings. I suspect I've missed something! If I want to get out text from a textblock and pass it along with the Action Message handler, what is the correct syntax? 

Mar 29, 2011 at 4:44 PM
Edited Mar 29, 2011 at 4:46 PM

Have you tried to name the controls differently than the binding?

 

e.g. <TextBlock x:Name="MyTitle" Text="{Binding Title}" />

and then call the Action with MyTitle.Text...

Mar 29, 2011 at 5:01 PM

Hi syggen,

I attempted your suggestion, but the outcome was the same, I really don't know what the problem is. It fires the event, and I can use it together with the navigationservice to launch another view. But as said, the parameters to the method are null

Do you have any other ideas?

I assume Action Messages do not require any setup, other than the default?

Mar 29, 2011 at 5:20 PM

I remember there has been discussions regarding the use of datatemplate. See this for example

http://caliburnmicro.codeplex.com/discussions/249941