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!

6 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

  4. Hi Jan,

    we came in trouble related to this issue too. The problem occurs in an event handler of a custom control.
    Oliver Meyer is speaking of “Although the DataWorkspace side of this issue is less commonly encountered, I might as well add on a quick overview of that, as it was relevant to the forum post. In simple terms, the DataWorkspace is what you use when dealing with data in Lightswitch, and one is created for you in the view model. However, when working in the screen’s code-behind, you don’t have one, which is why the error message shown at the top appears. If you need to access data from an event handler, you’ll probably need to grab your own DataWorkspace.” on his blog.
    And like that using “this.Details.Dispatcher.BeginInvoke” brings the solution.

    But the strange thing is that SOME PC’s are running in troubles (the NullReferenceException) and others DO NOT?!?

    What can be the reason of the different behaviour?!?
    My worry is that the problem comes up again (like it escalated this time without knowing the reason) there we developed several Lightswitch applications.
    Also my worry is that there are no problems on the developer’s pc and we have no idea when it will occur in production on the enduser’s pc.
    So the big question: where is the consistency?!?

    Kind regards,

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