LightSwitch hacking challenge: fix the NullReferenceException

I’ve been posting so much about the awesome LightSwitch HTML client lately (have you seen the second season of Leading LightSwitch by the way?) that you might get the impression that I’m now an HTML-Hipster exclusively…

No way, and as a tribute to my love to the strongly typed world (also just because it’s really cool) I’m going to challenge your LightSwitch haxpert level with a coding riddle…

I have a normal LightSwitch screen, that has a local property (from Screen Designer > Add data item > Local property: UserRegistration(Entity) ) and a fancy custom user control.  From the screen’ Created method, I’m trying to bind an event handler to an event on this fancy custom user control that is supposed to instantiate the local UserRegistration property, then show a modal popup.  Nothing extraordinary, one would think when looking at the code:

namespace LightSwitchApplication
public partial class SchedulingViewDiagram
partial void SchedulingViewDiagram_Created()
this.FindControl("ReallyCoolImageButton").ControlAvailable += SchedulingViewDiagram_ControlAvailable;
void SchedulingViewDiagram_ControlAvailable(object sender, ControlAvailableEventArgs e)
var reallyCoolImageButton = e.Control as ReallyCoolImageButton;
reallyCoolImageButton.Clicked += (s,evt ) => {
this.NewUserRegistration = this.DataWorkspace.ApplicationData.UserRegistrations.AddNew();
this.NewUserRegistration.RegistrationAdded = DateTime.Now;

This code actually throws a NullReferenceException at line 15 (much to my own initial surprise)…  I verified this by putting up a breakpoint, and indeed, ‘this.NewUserRegistration’ returns null even though we just assigned the return value of the AddNew method on line 14!!

Your mission, should you choose to accept it: explain why, and propose a fix.  The rules of the challenge are simple: the first one to post the correct answer wins, he/she doesn’t actually win anything (I’m completely broke after giving away $10.000 last month), and you are excluded from entering the competition if your name is Matt Evans, Huy Nguyen, … ( or if you share a whiteboard with any of these guys).

As always, should any member of your team be caught or killed, the Secretary will disavow all knowledge of your actions.

Keep rocking LS!

6 thoughts on “LightSwitch hacking challenge: fix the NullReferenceException

  1. Hi Jan
    I’ll have a shot at the issue when I get some spare time, but am interested in your “fancy custom user control” for registrations.
    Can you please share details on how to check it out.
    Thanks in advance, Mark.

  2. Pingback: LightSwitch hacking challenge: fix the NullReferenceException: sample | Jan Van der Haegen's blog

  3. why not use

    (e.Control as ReallyCoolImageButton).Click += (s,evt) => {

    this.Details.Dispatcher.BeginInvoke(() =>
    this.LocalStringProperty = “Hello world”;
    this.ShowMessageBox(“Local string property is: ” + this.LocalStringProperty);


  4. Pingback: LightSwitch Community & Content Rollup– May 2013 (+more) - Beth Massi - Sharing the goodness - Site Home - MSDN Blogs

  5. Pingback: Beth Massi的中文博客

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s