we're having this big problem with our application. It's a rather large application with several modules, and thousands and thousands lines of code. A lot of parts of the application are designed to exist only with a reference to another object, for example a Person object can never exists without a House object, so if you at any point in the app say:
bool check = App.Person.House == null;
check should always be false (by design), so, to keep using that example, while creating modules, testing, debugging, App.Person.House is never null, but once we shipped the application to our client, they started getting a bunch of NullReferenceException with the objects that by design, should never have a null reference. They tell us the bug, we try to reproduce it here, but 90% of the times we can't, because here it works fine.
The app is being developed with C# and WPF, and by design, it only runs on Windows XP SP 3, and the .net framework v3.5, so we KNOW the user has the same operative system, service pack, and .net framework version as we do here, but they still get this weird NullReferenceExceptions that we can't reproduce.
So, I'm just wondering if anyone has seen this before and how you fixed it, we have the app running here at least 8 hours a day in 5 different computers, and we never see those exceptions, this only happens to the client for some reason.
ANY thought, any clue, any solution that could get us closer to fixing this problem will be greatly appreciated.
Thanks!
Well, you haven't really told us much about the House property... is it writable? If so, put validation into the setter which at least logs if the value is null - and ideally throw an ArgumentNullException immediately (I believe it's generally better to stop when your data is corrupt rather than continuing and hoping life will sort itself out). If it's just set to a backing field in a constructor, check it there - and again, throw ArgumentNullException if it's null.
If it's computed in some way, that makes it harder - at that point you should probably test in the getter and log (in some way that will be easy for your customers to get the information back to you) as much information as you think might be relevant.
EDIT: As has been pointed out, this could apply at any level within the expression - so you may well want to apply the same sort of validation and logging to each level.
The line
bool check = App.Person.House == null;
will throw a null-ref exception when either App or App.Person is null.
You say 'cannot be null by design' but that is impossible to verify from your description. It seems clear though that you user(s) simply follow a path through your program that is not covered in your test-cases.
If you cannot spot the flaw in your design then the practical approach would be to extend your app with internal checking. I would recommend a good tracing system and (a lot of) System.Diagnostics.Trace.Assert(xx != null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With