| Doc Type | Tips & Tricks |
| Email Setting | Make Public |
| Email Address | steve dot robinson at notes411 dot com |
| Keep informed? | Yes |
| Author | Steve C Robinson |
| Company Name | Notes411 |
| Category | .NET, Web Application |
| Modified | 06/09/2006 11:17:50 |
| Subject | Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. |
You are writing a web application and you decide
to alter your global.asx file. When you try to debug this application you
find you can an error in the code below.
protected void
Application_Start(Object sender, EventArgs e)
{
Application["AppplicationCount"] = 0;
Application["SessionCount"] = 0;
Application["AppplicationCount"] = (int)Application["AppplicationCount"]
+ 1;
}
protected void
Session_Start(Object sender, EventArgs e)
{
Application["SessCount"] = (int)Application["SessCount"]+1;
Response.Write("Number of Applications: " +
Application["AppplicationCount"] + "< /br>");
Response.Write("Number of Sessions: " +
Application["SessCount"] + "< /br>");
}
Error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference
not set to an instance of an object.
Source Error:
Line 32:
protected void
Session_Start(Object sender, EventArgs e)
Line 33:
{
Line 34:
Application["SessCount"]
= (int)Application["SessCount"] + 1;
Line 35:
Response.Write("Number of
applications: " +
Line 36:
Application["AppCount"]
+ "< /br>"); |
Solution:
The problem is that we are referencing an object that doesn't exist or
is out of scope. The problem here was an Application variable SessionCount
was spelt wrong in the Session_Start
method. It was Application["SessCount"] when it should have been
Application["SessionCount"]
|