Sentry Answers>C#>

What is a NullReferenceException in C# and How do I Fix it?

What is a NullReferenceException in C# and How do I Fix it?

Dean R.

The ProblemJump To Solution

Here’s a simple example of code that generates a NullReferenceException.

Click to Copy
using System; class Solution { static void Main(String[] args) { int[] n = null; Console.WriteLine(n[0]); } }

Here we get a NullReferenceException because the integer array n is a null pointer, so it makes no sense to try to print its 0th element.

Of course, when a NullReferenceException appears in your code, chances are it will not be nearly as obvious. Let’s take a look at a slightly more sinister example:

Click to Copy
using System; class Birthday { public int day { get; set; } public int month { get; set; } public int year { get; set; } } class Person { public Birthday birthday { get; set; } } class Solution { static void Main(String[] args) { Person bob = new Person(); bob.birthday.day = 1; bob.birthday.month = 1; bob.birthday.year = 1970; } }

This code also generates a NullReferenceException, but why? We’ve clearly defined the day, month, and year of Bob’s birthday, so why is the compiler still yelling at us, and how can we fix it?

The Solution

In C#, objects must be instantiated before they are referenced.

In our example, even though we defined all of the necessary parts of Bob’s birthday, we didn’t instantiate the birthday itself. By defining a separate class for Birthday, we are claiming that a birthday is more than just the day, month, and year that make it up. As such, our current implementation is missing something.

There are two ways of solving this problem: the additive method and the subtractive method.

Method 1: The Additive Method

If we want to maintain our claim that a birthday is its own concept greater than the sum of its parts, then we can code our solution accordingly by adding an instantiation of our Birthday object. Keeping all else equal, add the line Birthday b = new Birthday(); to Main() , like this:

Click to Copy
static void Main(String[] args) { Person bob = new Person(); Birthday b = new Birthday(); // instantiate new Birthday object b.day = 1; b.month = 1; b.year = 1970; }

Method 2: The Subtractive Method

Another, perhaps more logical, way to think about it is that a birthday is not more than the sum of its parts. In that case, we can eliminate the Birthday class altogether and simply assign the components of Bob’s birthday directly to Bob. That would look something like this:

Click to Copy
using System; class Person { public int birthday_day { get; set; } public int birthday_month { get; set; } public int birthday_year { get; set; } } class Solution { static void Main(String[] args) { Person bob = new Person(); bob.birthday_day = 1; bob.birthday_month = 1; bob.birthday_year = 1970; } }

Tracking down these issues can be tricky, but as long as you remember that a NullReferenceException means you tried to work with something before you made sure that it exists, you should be able to debug any such errors without too much headache.

  • SentryC# Error and Performance Monitoring
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.