1

In my code I have a variable of type int. Directly after initalizing it, I receive a NullReferenceException. I am stumped why this is happening and actually how that is even possible.

Here is the code:

int lookupValue = 0;
if (0 == lookupValue)

And here is the debugger screen. The value of lookupValue is actually 0.

Luke Wage
  • 693
  • 4
  • 13
  • one of the following lines throws the exception, not if – ASh Mar 17 '15 at 08:35
  • Are you sure about that? What is your stack trace exactly? Is it points that line? – Soner Gönül Mar 17 '15 at 08:35
  • tested your code and works fine. The issue is somewere else – Sim1 Mar 17 '15 at 08:37
  • post the rest of the code please – Sim1 Mar 17 '15 at 08:37
  • it seems there is nothing wrong with your code .. i dont know why its throwing nullerference exception .. i tested and it works fine for me on .net 4.5.1 – AVIK DUTTA Mar 17 '15 at 08:37
  • 4
    Recompile in debug mode. That's not the line that causes the error. – Tim Schmelter Mar 17 '15 at 08:38
  • Based on: [MSDN](https://msdn.microsoft.com/es-es/library/s1ax56ch.aspx), if your problem is in fact the int var, try to instantiate it first and check if it exists before giving it the value. – Btc Sources Mar 17 '15 at 08:41
  • 1
    I tried to re-create the bug..it is not possible.I think it is from some other part of the code. – Justin CI Mar 17 '15 at 08:58
  • 1
    Run the code in a debugger, set a breakpoints before the problem, and then single step until the Exception. I **expect** the issue with be within a few lines of the "if block" most lickly the statement just after it. – Ian Ringrose Mar 17 '15 at 09:22
  • 1
    Also do a COMPLETE rebuild, by deleting all the DLLS and rebooting the machine you are running on - you may have a dlls that no longer much your source code – Ian Ringrose Mar 17 '15 at 09:23
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine Oct 04 '15 at 09:09

2 Answers2

3

The debugger is showing the wrong line as the exception source. This does happen sometimes, you need to keep an eye on the surrounding code, and the stack trace.

Since you're working with a web application, it's also quite possible that the debugging information is out of sync with the code. Rebuilding the whole project might help, unless your dependencies are badly arranged.

Look at code ahead of the comparison, and below it as well (Is Session null? Is Session.UserId null? Is SqlCommands.LookupInsertCommand throwing NullReferenceException?). You can use quick watch to check pieces of code and find the one causing the NullReferenceException.

As a side-note, try not to carry practices from other languages to C#. Initialize local variables when you actually have a reasonable value to initialize them with - don't worry, the compiler will not allow you to compile code reading a variable that hasn't been assigned yet. When you just assign a default value, you're losing out on a few sanity checks of the code. Also, don't compare constant == variable. There's no reason to do that in C#, because you can't just accidentally type variable = constant - it will not compile (the only exception being the bool type, but you shouldn't compare that to a constant anyway - just do if (boolValue) or if (!boolValue)). It just makes the code harder to read and understand.

EDIT:

This case in particular is actually quite obvious if you know what you're looking for. You see, the if (0 == lookupValue) doesn't exist anywhere in the compiled binary - the compiler can safely ignore it, because lookupValue will always be 0. Usually, the debugging information will account for this, but missing by one line is quite common even when there's nothing as drastic as a whole missing line of code (in your case, likely more than one).

Since you are working with an ASP.NET application, part of the code isn't actually compiled by Visual Studio - it's compiled when you make a request. To generate proper debug information, you must also set the <compilation debug="true" /> in web.config (Compilation element).

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • I'll mark this answer for now as accepted. Looks like I need to rebuild and clean up the whole solution. Some more investigation has shown the problem to be somewhere else, as some other posters mentioned. Thanks everybody for confirming that the debugger message indded made no sense at the point where it was shown. – Luke Wage Mar 17 '15 at 09:27
  • Yep, even after a complete cleanup and rebuild and reboot of the machine, the IDE (running in debug mode) did not show the faulty line, which was in a different method. – Luke Wage Mar 17 '15 at 11:23
  • @LukeWage See my update - your line doesn't actually make it into the binary at all, it can be statically determined it will never change the outcome. – Luaan Mar 17 '15 at 13:13
  • Thanks for your extensive help. It is actually a WinForm app. Don't let the Session statement fool you. :-) And in production there is way more code betweeen the declaration and the if statement. I removed it to be more concise in my code. – Luke Wage Mar 17 '15 at 13:41
  • @LukeWage So you actually got the exception to show up in the simplified code as well with the same result? Interesting. In any case, I wouldn't worry myself too much over it - it's just something that happens. Just remember that the line shown isn't necessarily *exactly* the line where the error happened, especially if you're doing some postprocessing on the binaries (obfuscation, optimization, code generation). – Luaan Mar 17 '15 at 13:51
-3

There is no error in your code. anyway try this...

if (lookupValue.CompareTo(0) == 0)
Nalaka
  • 1,165
  • 7
  • 12
  • 1
    If there's no error in the code, implementing an alternative that is less concise code won't make any difference. – Dan Puzey Mar 17 '15 at 09:12