RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Null Reference Exceptions: Why & How to Prevent

Vorspire

Knight
Null Reference Exceptions: Why & How to Prevent
Difficulty: 2/10 (Requires basic understanding of key-words, data-types and variables)​
This tutorial will teach you how to distinguish and fix potential Null Reference Exceptions.​


A NullReferenceException is thrown when the code attempts to access a non-static member (method, property) of an object that has no value set.
This only pertains to Nullable Types, variables whose' Type supports being 'null' (empty, undefined, not set)

Structs and simple data-types are exempt;

  • Bool
    • Boolean
    • Default: false
    • Range: false, true
  • Char
    • Unicode Character Int-16
    • Default: U+0000
    • Range: U+0000 to U+ffff
  • SByte
    • Signed Int-8
    • Default: 0
    • Range: -128 to 127
  • Byte
    • Unsigned Int-8
    • Default: 0
    • Range: 0 to 255
  • Short
    • Signed Int-16
    • Default: 0
    • Range: -32,768 to 32,767
  • UShort
    • Unsigned Int-16
    • Default: 0
    • Range: 0 to 65,535
  • Int
    • Signed Int-32
    • Default: 0
    • Range: -2,147,483,648 to 2,147,483,647
  • UInt
    • Unsigned Int-32
    • Default: 0
    • Range: 0 to 4,294,967,295
  • Long
    • Signed Int-64
    • Default: 0
    • Range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • ULong
    • Unsigned Int-64
    • Default: 0
    • Range: 0 to 18,446,744,073,709,551,615
  • Double
    • Floating-Point Precision 15-16 Digits
    • Default: 0.0d
    • Range: ±5.0 × 10 ^ −324 to ±1.7 × 10 ^ 308
  • Decimal
    • Floating-Point Precision 28-29 Significant Digits
    • Default: 0.0m
    • Range: ±1.0 × 10 ^ −28 to ±7.9 × 10 ^ 28
  • Float
    • Precision 7 Digits
    • Default: 0.0f
    • Range: ±1.5 × 10 ^ −45 to ±3.4 × 10 ^ 38


Here's an example of how to generate a NullReferenceException, and how to fix it:
(NullReferenceExceptions tend to follow the same pattern, and are by far the easiest exception to trace and fix)

Code:
string hello = null;

Console.WriteLine( "The String variable 'hello' contains {0} Chars.", hello.Length );

In the above code, the NullReferenceException is thrown when it tries to access 'hello.Length', if 'hello' has a value of 'null', then it won't have a reference to any property named 'Length' and thus crashes the application.

In order to fix the issue, you check if 'hello' is null before you access its' members (the Length property);

Rich (BB code):
string hello = null;

if( hello != null )
      Console.WriteLine( "The String variable 'hello' contains {0} Chars.", hello.Length );
else
      Console.WriteLine( "The String variable 'hello' has not been set.", hello.Length );

It's good practice to 'null-check' all Nullable types before you access them; by following he guide at the top of this post, you will have a general idea of what needs a null-check, but just while you're getting used to it, assume EVERYTHING can be null and write a null-check for it anyway. If you try to do a null-check on a non-Nullable type, you will get a compile error.

if you have Visual Studio, I highly recommend you install ReSharper, it will point out where possible null-checks have been missed amongst many hundreds of other amazing features that will make your development life easier (and save time posting for help on trivial issues).

You can also read up more about NullReferenceExceptions on MSDN:
http://msdn.microsoft.com/en-us/library/system.nullreferenceexception(v=vs.71).aspx
 

m309

Squire
Great tutorial, Vorspire. ReSharper sounds amazing, any recommendations for something comparable that will operate in conjunction with VS Express? ReSharper only works with the full edition of VS. Thanks!
 

Vorspire

Knight
I just realised after all this time the last example was wrong, the last line contains a reference to 'hello.Length' when it is not needed (which will still crash the application, lol)
 
Top