Wednesday, 15 April 2015

c# - NullReferenceException on value type -


In my code I have a type of variable int then after initalizing it directly, I receive a NullReferenceException. I am getting stumpy why this is happening and how it is actually possible.

Here is the code:

  int lookupValue = 0; If (0 == lookup value)  

and there is a debugger screen here. The value of LookupValue is actually 0.

Debugger is showing the wrong line as an exception source. This happens occasionally, you have to keep an eye on the surrounding code and stack trace.

Since you are working with a web application, it is also possible that debugging information is out of sync, with the code, until your dependency is badly organized, Rebuilding may be possible.

Look at the forward code in comparison to the comparison, and also below it (what is session null? Is Session.UserId null? What SqlCommands.LookupInsertCommand throwing NullReferenceException ?). You can check the code pieces and NullReferenceException

Start local variable When you really have a fair value to start with - do not worry, the compiler will let you read a similar variable Will not allow code compiling that has not yet been specified. When you only give a default value, then you are missing out on some discretion of the code. Also, do not compare constant == variables . There is no reason to do this in C #, because you can not just type variable = constant by mistake - it will not be compiled (only the exception of bool type, But you should not compare any kind of continuous - just if (the root value) or if (! BoolValue) ) It just makes a hard code to read and understand.

Edit:

Especially this case is really quite obvious if you know what you are looking for. If the compiled is not available anywhere in the binary, then if (0 == lookup volumes) - the compiler can safely ignore it, because lookupValue always Ho 0 . Generally, information about debugging will be for this, but it is very common to be missing from one line, even when the complete missing line of code (such as in your case, more likely than one) is not as rigid as it is.

Since you are working with an ASP.NET application, the part of the code is not actually compiled by Visual Studio - when you make a request it is compiled to generate proper debug information. To do this, you must & lt; Compilation debug = "true" /> You must set web.config () in .


No comments:

Post a Comment