LightSwitch hacking challenge: fix the NullReferenceException: sample

Yesterday I came across a rather interesting NullReferenceException, which occurred right after I set a local screen property to a particular value (non-null) then (in the very next line of code) retrieved that property to do some additional work.

Fixing it is easy ONCE you know what causes this odd behavior.  I can promise you that there’s not one single written resource available on the web about this, making it a very blogworthy subject.  Instead of going straight to the explanatory post though, I thought some readers would enjoy a riddle first (I certainly would), hence the hacking challenge.

To avoid confusion over the fact that I’m using a fancy user control or showing a modal dialog, I’ve uploaded a working code sample. You can download it, or just study the sources online.  In the sample, I’ve reduced the fancy control to an absolute minimum, to avoid any confusion (for example: that the NRE is caused by the fanciness of the control). I’ve also rewritten the original problem a bit to avoid confusion about the modal dialog or use of an entity.



I added an additional hint in the sample.  To see this hint, simple open the second screen (in the application/code).


Good luck, and keep rocking LS!

About these ads

5 thoughts on “LightSwitch hacking challenge: fix the NullReferenceException: sample

  1. Hehe, I’ve been working so much with the HTML Client lately, I’d _almost_ forgotten about those issues…. it could be a real pain remembering which [redacted to avoid spoiler] was the right one to use for a given operation.

  2. Hi Jan,
    thanks for the head-nut; I learned about it from Mr Yossu, and then I tried it with Details.Dispatcher instead of Main-Dispatcher
    void OriginalProblem_ControlAvailable(object sender, ControlAvailableEventArgs e)
    this.FindControl(“ReallyCoolImageButton”).ControlAvailable -= OriginalProblem_ControlAvailable;
    (e.Control as ReallyCoolImageButton).Click += (s, evt) =>
    this.Details.Dispatcher.BeginInvoke(() =>
    this.LocalStringProperty = “Hello world”;
    this.ShowMessageBox(“Local string property is: ” + this.LocalStringProperty);
    and don’t forget to remove the handler in ControlAvailable.

    I knew I must learn about this, when I first read Mr Yossus page, I did not understand quite all, but now it gets more clear and clear

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

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 )

Google+ photo

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

Connecting to %s