LightSwitch hacking challenge: fix the NullReferenceException: sample
May 22, 2013 5 Comments
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.
BIG BIG SPOILER ALERT!
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!