Friday, March 16, 2012

Object reference not set to an instance of an object.

Im not understanding why I am getting this error. It shows on all of my .aspx pages. As soon as I add the the Page Language="C#". Here is the stack trace.

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String name) +1618
System.Web.Security.WindowsAuthenticationModule.OnEnter(Object source, EventArgs eventArgs) +593
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

If it shows only when you add that line, check the syntax and make sure that the code really is C#.

Jeff


That usually happens if you do something like this:
string s = this.Request["someKey"];
s = s.Trim();
since, if the key is not found, s is null, and cannot be used as a string (s = s.Trim()).

The correct way, so that you don't get exceptions:
string s = this.Request["someKey"];
if ( s != null )
s = s.Trim();
else
s = string.Empty;

In other words, do not use the string class (or any other class for that matter) functions and properties until you make sure that the variable is not null.

NC...

0 comments:

Post a Comment