46

We're launching a system, and we sometimes get the famous exception NullReferenceException with the message Object reference not set to an instance of an object.

However, in a method where we have almost 20 objects, having a log which says an object is null, is really of no use at all. It's like telling you, when you are the security agent of a seminar, that a man among 100 attendees is a terrorist. That's really of no use to you at all. You should get more information, if you want to detect which man is the threatening man.

Likewise, if we want to remove the bug, we do need to know which object is null.

Now, something has obsessed my mind for several months, and that is:

Why doesn't .NET give us the name, or at least the type of the object reference, which is null?. Can't it understand the type from reflection or any other source?

Also, what are the best practices to understand which object is null? Should we always test nullability of objects in these contexts manually and log the result? Is there a better way?

Update: The exception The system cannot find the file specified has the same nature. You can't find which file, until you attach to the process and debug. I guess these types of exceptions can become more intelligent. Wouldn't it be better if .NET could tell us c:\temp.txt doesn't exist. instead of that general message? As a developer, I vote yes.

Cloudy
  • 703
Saeed Neamati
  • 18,318

8 Answers8

29

The NullReferenceException basically tells you: you are doing it wrong. Nothing more, nothing less. It's not a full-fledged debugging tool, on the contrary. In this case I'd say you're doing it wrong both because

  • there is a NullReferenceException
  • you didn't prevent it in a way you know why/where it happened
  • and also maybe: a method requiring 20 objects seems a bit off

I'm a big fan of checking everything before things start going wrong, and providing good information to the developer. In short: write checks using ArgumentNullException and the likes and write the name yourself. Here's a sample:

void Method(string a, SomeObject b)
{
    if (a == null) throw ArgumentNullException("a");
    if (b == null) throw ArgumentNullException("b");
// See how nice this is, and what peace of mind this provides? As long as
// nothing modifies a or b you can use them here and be 100% sure they're not
// null. Should they be when entering the method, at least you know which one
// is null.
var c = FetchSomeObject();
if(c == null)
{
    throw InvalidOperationException("Fetching B failed!!");
}

// etc.

}

You could also look into Code Contracts, it has it quirks but it works pretty well and saves you some typing.

update in .Net 6 there's ThrowIfNull which removes the need for stating the parameter name. And since C# version 6, there's the nameof operator removing the rather error-prone need to repeat the argument name in a hardcoded string.

void Method(string a, SomeObject b)
{
  //.Net 6
  ArgumentNullException.ThrowIfNull(a);

//C# 6 if (b == null) { throw new ArgumentNullException(nameof(b)); } }

As to why this is not the default: we can only guess (or try to dig up notes somehow from people working on C# and/or .Net). My guess:

  • sometimes it's not needed or wanted
  • sometimes the message should have more than just the parameter name
  • there are alternatives (stacktrace, adding it manually)
  • it gives developers a choice
  • adding this everywhere by default isn't cheap because it means nearly all argument and variable names must be added as strings in the executable
stijn
  • 4,168
23

It really should show exactly what you are trying to call. It's like saying "There's a problem. You need to fix it. I know what it is. I'm not going to tell you. You go figure it out" Bit like half the answers on this Stack Overflow, ironically.

So how useful would it be, for example, if you got this...

Object reference (HttpContext.Current) not set to instance of an object

...? To have to go into the code, step through it, and work out that the thing you are trying to call is null is fine, but why not just give us a little helping hand?

I agree that it is usually useful to step through the code to get to the answer (because you will probably find out more), but often a lot of time and frustration would be saved if the NullReferenceException text was more like the example above.

Just sayin.

4

Your log should include a stack trace - that usually gives you a hint as to which line in the method has the problem. You may need to make your release build include PDB symbols so you have an idea which line the error is on.

Granted, it won't help you in this case:

Foo.Bar.Baz.DoSomething()

The tell don't ask principle can help to avoid such code.

As to why the information isn't included, I'm not sure - I suspect that at least in a debug build, if they really wanted to, they could figure it out. Taking a crash dump and opening in WinDBG may help.

Paul Stovell
  • 1,719
2

Exceptions have been created as a tool to signal exceptional non-fatal conditions up the call chain. That is, they are not designed as a debugging tool.

If a null-pointer exception were a debugging tool, it would abort program execution right on the spot, allowing a debugger to connect, pointing it right at the incriminating line. This would give the programmer all the context information that is available. (Which is pretty much what a Segfault due to a null pointer access does in C, albeit a bit crudely.)

However, the null-pointer exception is designed as a valid runtime condition that can be thrown and caught in normal program flow. Consequently, performance considerations need to be taken into account. And any customization of the exception message requires string objects being created, concatenated, and destroyed at runtime. As such, a static message is undisputably faster.

I'm not saying that the runtime could not be programmed in a way that would yield the name of the incriminating reference, though. That could be done. It would just make exceptions even slower than they are. If anyone cared enough, such a feature could even be made switchable, so that it would not slow down production code, but allow for easier debugging; but for some reason nobody seems to have cared enough.

1

Cromulent hit the nail on the head I think, however there is the obvious point too that if you're getting a NullReferenceException you have uninitialised variable(s). The argument that you have circa 20 objects being passed into a method cannot be said to be a mitigation: as the creator of a chunk of code you have to be responsible for its doings, that includes its compliance to the rest of a codebase, as well as proper and correct utilisation of variables etc.

it is onerous, tedious and sometimes dull, but the rewards at the end are worth it: many are the times when I have had to trawl through log files weighing in at several gigabytes, and they are almost always helpful. However before you get to that stage, the debugger can help you, and before that stage good planning will save a lot of pain(and I do not mean a fully engineered approach to you code solution either: simple sketches and some notes can and will be better than nothing).

In regards to the Object reference not set to an instance of an object the code cannot guess at values we may like: that's our job as programmers, and it simply means you have passed an uninitialised variable in.

GMasucci
  • 246
  • 2
  • 6
1

The reason that most languages don't provide this is that it's more work.

The compiler breaks up an expression like A.b.c into two operations:

  1. Get static field A.b
  2. Get field c

The runtime virtual machine executes each one separately. First it gets A.b and sees the result is null. Then it gets null.c. Oops! It throws a NullReferenceException saying you can't get null.c.

Although it's immediately obvious, when executing operation 2, that the thing on the RHS was .c it's not obvious the thing on the left was A.b - it could have been a very complicated expression involving, for example, function calls or ternary operators. Working backwards from the individual operations is not easy, and the only benefit is a slightly more precise error message.

Some language implementations put in the effort to work backwards some of the time (like if the immediately preceding operation is another "get field"); most don't bother. Lua and Java got this feature in the last several years.

0

Learn to use the debugger. This is exactly the kind of thing it is designed for. Set a break point on the method in question and away you go.

Simply step through your code and see exactly what the values of all your variables are at certain points.

Edit: Frankly I'm shocked that no one else has mentioned using the debugger yet.

Cromulent
  • 947
  • 1
  • 6
  • 13
0

If you wanted to go with @stijn's answer and put null checks in your code, this code snippet should help. Here's some info about code snippets. Once you have this set up, just type argnull, hit tab twice, and then fill in the blank.

<CodeSnippet Format="1.0.0">
  <Header>
    <Title>EnsureArgNotNull</Title>
    <Shortcut>argnull</Shortcut>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>argument</ID>
        <ToolTip>The name of the argument that shouldn't be null</ToolTip>
        <Default>arg</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[if ($argument$ == null) throw new ArgumentNullException("$argument$");$end$]]>
    </Code>
  </Snippet>
</CodeSnippet>