Dean R.
—Here’s a simple example of code that generates a NullReferenceException
.
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:
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?
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.
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:
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; }
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:
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.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.