Friday, March 16, 2012

Object reference not set to an instance of an object.

Hi.
I'm following along with the learnvisualstudio.net videos. Everything
works in VWD but, when I upload my files to the web server, the pages
for the 3rd and 4th videos don't work.
For both pages, I'm getting an "Object reference not set to an
instance of an object." error message.
Video 3
Is erroring on "Page.Response.Write("User Count: " &
Application("userCount").ToString())".
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
v_lesson03.Page_Load(Object sender, EventArgs e) in C:\DATA\webs\jtw
\MySite.com\VideoLesson3\v-lesson03.aspx.vb:17
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1436
Video 4
Is erroring on "resultLabel1.Text = Session("SomeValue").ToString()".
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
_04_v_lesson04.Button1_Click(Object sender, EventArgs e) in C:\DATA
\webs\jtw\MySite.com\VideoLesson4\v-lesson04.aspx.vb:8
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String
eventArgument) +7
System.Web.UI.Page. RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+33
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1746
Does anyone have any suggestions I could try to solve these problems.
TIA,
j.t.wHi,
Application("userCount") might be "null" and calling ToString method on a
null object would throw the null reference exception.
Would you consider checking if it is null or using explicit conversion?
Either:
if (Application("userCount") != null)
Application("userCount").........
Or:
Application("userCount") as string;
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk
"j.t.w" <j.t.w@.juno.com> wrote in message
news:c84f14fb-7cbf-4dd1-8376-31bbf7e1f417@.m34g2000hsb.googlegroups.com...
> Hi.
> I'm following along with the learnvisualstudio.net videos. Everything
> works in VWD but, when I upload my files to the web server, the pages
> for the 3rd and 4th videos don't work.
> For both pages, I'm getting an "Object reference not set to an
> instance of an object." error message.
> Video 3
> Is erroring on "Page.Response.Write("User Count: " &
> Application("userCount").ToString())".
> Stack Trace:
> [NullReferenceException: Object reference not set to an instance of an
> object.]
> v_lesson03.Page_Load(Object sender, EventArgs e) in C:\DATA\webs\jtw
> \MySite.com\VideoLesson3\v-lesson03.aspx.vb:17
> System.Web.UI.Control.OnLoad(EventArgs e) +99
> System.Web.UI.Control.LoadRecursive() +47
> System.Web.UI.Page.ProcessRequestMain(Boolean
> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
> +1436
> Video 4
> Is erroring on "resultLabel1.Text = Session("SomeValue").ToString()".
> Stack Trace:
> [NullReferenceException: Object reference not set to an instance of an
> object.]
> _04_v_lesson04.Button1_Click(Object sender, EventArgs e) in C:\DATA
> \webs\jtw\MySite.com\VideoLesson4\v-lesson04.aspx.vb:8
> System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
> System.Web.UI.WebControls.Button.RaisePostBackEvent(String
> eventArgument) +107
> System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.Raise
PostBackEvent(String
> eventArgument) +7
> System.Web.UI.Page. RaisePostBackEvent(IPostBackEventHandler
> sourceControl, String eventArgument) +11
> System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
> +33
> System.Web.UI.Page.ProcessRequestMain(Boolean
> includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
> +1746
>
> Does anyone have any suggestions I could try to solve these problems.
> TIA,
> j.t.w
Coskun (and other gurus),
Thank you for your response. You were right; the
Application("userCount") is null.
So, my next problem seems to be that since the
Application("userCount") is null, the aspx page is not seeing the
Global.asax page where the userCount is being set.
Is there a way to connect the Global.asax to the aspx page? Or... Is
there a way for the web site to recognize the Global.asax page
automatically?
FYI. These are the sub procedures that are in my Global.asax page.
Sub Application_Start(ByVal sender As Object, ByVal e As
EventArgs)
Application("userCount") = 0
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Application("userCount") += 1
Session.Add("SomeValue", Session.SessionID)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Application("userCount") -= 1
End Sub
Again, Thank you for any help you can provide.
j.t.w
Hi,
There is not a special way of registering your Global.asax file. It should
be used by your web application as long as it is in the root folder.
If you think that it does not work for some reason - that doesn't come to my
mind now - you may want to create a basic HttpModule implementation of yours
and do anything you can do in a Global.asax file inside your HttpModule.
Please read the article at http://support.microsoft.com/kb/307996
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk
"j.t.w" <j.t.w@.juno.com> wrote in message
news:e556e362-f2cc-47b9-bef0-ec8dea4eb931@.b2g2000hsg.googlegroups.com...
> Coskun (and other gurus),
> Thank you for your response. You were right; the
> Application("userCount") is null.
> So, my next problem seems to be that since the
> Application("userCount") is null, the aspx page is not seeing the
> Global.asax page where the userCount is being set.
> Is there a way to connect the Global.asax to the aspx page? Or... Is
> there a way for the web site to recognize the Global.asax page
> automatically?
> FYI. These are the sub procedures that are in my Global.asax page.
> Sub Application_Start(ByVal sender As Object, ByVal e As
> EventArgs)
> Application("userCount") = 0
> End Sub
> Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
> Application("userCount") += 1
> Session.Add("SomeValue", Session.SessionID)
> End Sub
> Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
> Application("userCount") -= 1
> End Sub
> Again, Thank you for any help you can provide.
> j.t.w

0 comments:

Post a Comment