Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 606421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:15:09+00:00 2026-05-13T17:15:09+00:00

Apparently the vast majority of errors in code are null reference exceptions. Are there

  • 0

Apparently the vast majority of errors in code are null reference exceptions. Are there any general techniques to avoid encountering null reference errors?

Unless I am mistaken, I am aware that in languages such as F# is it not possible to have a null value. But thats not the question, I’m asking how to avoid null reference errors in languages such as C#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:15:09+00:00Added an answer on May 13, 2026 at 5:15 pm

    When a null reference exception is displayed to the user, this indicates a defect in the code resulting from an error on the part of the developer. Here are some ideas on how to prevent these errors.

    My top recommendation for people who care about software quality, and are also using the.net programming platform, is to install and use Microsoft code contracts ( http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx ). It includes capabilities to do run-time checking and static verification. The essential capability to build these contracts into your code is being included in the 4.0 version of the.net framework. If you are interested in code quality, and it sounds like you are, you may really enjoy using Microsoft code contracts.

    With Microsoft code contracts, you can guard 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 have recommended you do something like this

    if (customer == null) {throw new ArgumentNullException("customer");}
    

    Now I recommend

    Contract.Requires(customer != null);
    

    You can then enable the run-time checking system which will catch these defects as early as possible, leading you towards diagnosis and correction of the defective code. But don’t let me give you the impression that code contracts are simply a fancy way to replace argument null exceptions. They are much more powerful than that.
    With Microsoft code contracts, you can also run the static checker, and ask it to investigate possible sites in your code where null reference exceptions might occur. The static checker requires a bit more experience to use easily. I would not recommend it first for beginners. But feel free to try it out and see for yourself.

    RESEARCH ON THE PREVALENCE OF NULL REFERENCE ERRORS

    There has been some debate in this thread on whether null reference errors are a significant problem. A long-winded answer is below. For people who don’t want to wade through that, I will summarize.

    • Microsoft’s leading researchers in
      program correctness on the Spec# and
      code contracts projects believe it is
      a problem worth addressing.
    • Dr. Bertrand Meyer and the team of
      software engineers at ISE, who
      developed and support the Eiffel
      programming language, also believe it
      is a problem worth addressing.
    • In my own commercial experience developing ordinary software, I have seen null reference errors often enough, that I would like to address the problem in my own products and practices.

    For years, Microsoft has invested in research designed to improve software quality. One of their efforts was the Spec# project. One of the most exciting developments in my opinion with the.net 4.0 framework, is the introduction of Microsoft code contracts, which is an outgrowth of the earlier work done by the Spec# research team.

    Regarding your remark “the vast majority of errors in code are null reference exceptions”, I believe it is the qualifier “the vast majority” that will cause some disagreements. The phrase “Vast majority” suggests that perhaps 70-90% of faults have a null reference exception as the root cause. This seems far too high to me. I prefer to quote from the research of the Microsoft Spec#. In their article The Spec# programming system: An overview, by Mike Barnett, K. Rustan M. Leino, and Wolfram Schulte. In CASSIS 2004, LNCS vol. 3362, Springer, 2004, they wrote

    1.0 Non-Null Types Many errors in modern programs manifest themselves as
    null-dereference errors, suggesting
    the importance of a programming
    language providing the ability to
    discriminate between expressions that
    may evaluate to null and those that
    are sure not to (for some experimental
    evidence, see [24, 22]). In fact, we
    would like to eradicate all null
    dereference errors.

    This is a likely source for people at Microsoft who are familiar with this research. This article is available at the Spec# site.

    I’ve copied references 22 and 24 below, and included the ISBN for your convenience.

    • Manuel Fahndrich and K. Rustan M. Leino. Declaring and checking non-null types in an
      object-oriented language. In Proceedings of the 2003 ACM Conference on Object-Oriented
      Programming, Systems, Languages, and Applications, OOPSLA 2003, volume 38, number
      11 in SIGPLAN Notices, pages 302–312. ACM, November 2003. isbn = {1-58113-712-5},

    • Cormac Flanagan, K. Rustan M. Leino, Mark Lillibridge, Greg Nelson, James B. Saxe,
      and Raymie Stata. Extended static checking for Java. In Proceedings of the 2002 ACM
      SIGPLAN Conference on Programming Language Design and Implementation (PLDI), volume
      37, number 5 in SIGPLAN Notices, pages 234–245. ACM, May 2002.

    I reviewed these references. The first reference indicates some experiments they did reviewing their own code for possible null reference defects. Not only did they find several, but in many cases, the identification of a potential null reference indicated a broader problem with the design.

    The second reference does not provide any specific evidence for the assertion that null reference errors are problem. But the authors do state that in their experience, these null reference errors are significant source of software defects. The paper then proceeds to explain how they try to eradicate these defects.

    I also remembered seeing something about this in an announcement from ISE on a recent release of Eiffel. They refer to this issue as “void safety”, and like so many things inspired or developed by Dr. Bertrand Meyer, they have an eloquent and educational description of the problem and how they go about preventing it in their language and tools. I recommend you read their article http://doc.eiffel.com/book/method/void-safety-background-definition-and-tools to learn more.

    If you want to learn more about Microsoft code contracts, there are tons of articles that have arisen recently. You can also check my blog at http: SLASH SLASH codecontracts.info which is primarily devoted to conversations about software quality through the use of programming with contracts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apparently John Chambers added Reference Classes to R in version 2.12. There doesn't appear
Apparently there's a maximum number of arguments for std::thread for the std::thread implementation in
Apparently mutableCopy copies by reference, not value. Ie if I do this: NSMutableArray arrayA
Apparently, there's been a big brouhaha over whether or not Python needs tail-call optimization
Apparently there is a database postgres that is created by default on each postgresql
Apparently there is no predefined list available in .net. I'd like to use a
Apparently in VS2008 SP1 there's an option under Publish | Options to have a
Apparently, I can't find any help on this. I have a scenario where I
Apparently, this code gives me error at the last line cvCvtColor(), what is wrong?
Apparently there is a method that takes a char and returns a char: http://download.oracle.com/javase/6/docs/api/java/lang/Character.html#toLowerCase(char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.