When a null reference exception is displayed to the user, this indicates a code defect caused by an error on the part of the developer. Here are some ideas on how to prevent these errors.
My main recommendation for people who care about software quality and also use the .NET programming platform is to install and use Microsoft code contracts ( http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx ) . It includes runtime and static validation capabilities. A significant opportunity to create these contracts in your code is included in version 4.0. If you're interested in the quality of the code, and it sounds like you, you might really enjoy using Microsoft code contracts.
With Microsoft code contracts, you can protect your method from null values by adding preconditions like this "Contract.Requires (customer! = Null);". Adding a precondition like this is equivalent to the practice recommended by many others in their comments above. Prior to code contracts, I would recommend you do something like this.
if (customer == null) {throw new ArgumentNullException("customer");}
Now i recommend
Contract.Requires(customer != null);
Then you can activate the runtime verification system, which will catch these defects as soon as possible, which will lead you to diagnose and fix the defective code. But don't let me give the impression that code contracts are just a fancy way of replacing null arguments. They are much stronger. With Microsoft code contracts, you can also run a static check and ask her to examine possible sites in your code where null-reference exceptions may occur. Static validation requires a little more experience to use. I would not recommend it first for beginners. But feel free to try and see for yourself.
NULL REFERENCE ERROR DISTRIBUTION STUDY
There has been some debate in this thread about whether null reference errors are a serious problem. Below is a long answer. For people who do not want to get through this, I will summarize.
- Leading Microsoft researchers in the correctness of the program on Spe # and projects with code contracts consider a problem that deserves attention.
- Dr. Bertrand Meyer and the ISE software development team who developed and supported the Eiffel programming language, also believe this is a noteworthy issue.
- In my own commercial experience developing common software, I often saw null-referenced errors that I would like to solve the problem in my own products and practices.
For many years, Microsoft has invested in research aimed at improving software quality. One of their efforts was the Spe # project. One of the most exciting events, in my opinion, with the .NET 4.0 base is the introduction of Microsoft supply contracts, which is the result of earlier work done by the Spe # research group.
Regarding your remark, "the vast majority of errors in the code are null reference exceptions," I believe that this is the identifier of the "vast majority" that will cause some controversy. The phrase "The vast majority" suggests that perhaps 70-90% of errors have an exception with a null reference as the main reason. For me it is too high. I prefer to quote Microsoft SpeC # research results. SpeC # programming system in its article: A Review, Mike Barnett, C. Rustan M. Leino, and Wolfram Schulte. In CASSIS 2004 LNCS vol. 3362, Springer, 2004, they wrote
1.0 Non-empty types Many errors in modern programs manifest themselves as zero dereferencing errors, suggesting the importance of programming a language that distinguishes between expressions that can evaluate to null and those that are not necessary (for some experimental proofs, see [24, 22]). In fact, we would like to eradicate all dereferencing errors.
This is a likely source for people at Microsoft who are familiar with this study. This article is available on Spe # website.
I copied the links 22 and 24 below and included ISBN for your convenience.
Manuel Fahndrich and C. Rustan M. Leino. Declaring and checking non-empty types in an object-oriented language. In the proceedings of the 2003 ACM Conference on Object Oriented Programming, Systems, Languages, and Applications, OOPSLA 2003, Volume 38, Number 11 in SIGPLAN Notifications, pp. 302-312. ACM, November 2003. isbn = {1-58113-712-5},
Cormac Flanagan, C. Rustan M. Leino, Mark Lillybridge, Greg Nelson, James B. Sachs, and Raymie Stata. Extended static checking for Java. In the materials of ACM 2002, the SIGPLAN Conference on the Development and Implementation of the Programming Language (PLDI), volume 37, number 5 in SIGPLAN reports, pp. 234-245. ACM May 2002
I reviewed these links. The first link points to some experiments that they looked at for their own code for possible zero support defects. Not only did they find a few, but in many cases, identifying a potential null reference indicated a wider design problem.
The second link does not contain any specific evidence that null reference errors are a problem. But the authors claim that, in their experience, these zero reference errors are a significant source of software defects. The paper then explains how they are trying to eradicate these shortcomings.
I also recalled that I saw something about this in an ISE announcement of the recent release of Eiffel. They refer to this problem as “unmistakable security,” and, like many other things inspired or developed by Dr. Bertrand Meyer, they have an eloquent and educational description of the problem and ways to prevent it in their language and tools. I recommend that you read their article http://doc.eiffel.com/book/method/void-safety-background-definition-and-tools to find out more.
If you want to know more about Microsoft code contracts, a lot of articles have appeared recently. You can also check out my blog at http: SLASH SLASH codecontracts.info, which is mainly about talking about software quality using contract programming.