sessions, ascx, reference not set to an instance

A

Alexander Widera

hi,

i have the following problem in the following code:

Basket basket = (Basket) Session["Basket"];
if (basket.isEmpty)
{
...........
}


Basket is a class which i have created myself. It has a function isEmpty of
the type bool.

This code is in a ascx-page (usercontrol).

Now the problem. Sometimes I call a page, which includes the usercontrol all
works, but sometimes i get the errormessage "Object reference not set to an
instance of an object."
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Line 51: if (basket.isEmpty)



Why does this happen? Sometime it works and sometimes not?
Does somebody know this problem?


Thank you.
 
D

David Lloyd

Alexander:

You should always check the session variable for nullity prior to reading
its value. Apparently, in some cases, your session variable is returning
null.

For example:

if (Session["Basket"] != null) {
Basket basket = (Basket) Session["Basket"];
if (basket.isEmpty)
{
...........
}
}


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


hi,

i have the following problem in the following code:

Basket basket = (Basket) Session["Basket"];
if (basket.isEmpty)
{
...........
}


Basket is a class which i have created myself. It has a function isEmpty of
the type bool.

This code is in a ascx-page (usercontrol).

Now the problem. Sometimes I call a page, which includes the usercontrol all
works, but sometimes i get the errormessage "Object reference not set to an
instance of an object."
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Line 51: if (basket.isEmpty)



Why does this happen? Sometime it works and sometimes not?
Does somebody know this problem?


Thank you.
 
A

Alexander Widera

hmm ... ok .... thank you for that solution....
but I don't understand why the session is sometimes null and sometimes not
..... nothing changed ... I can click on a link to this page 100 times ...
again and again and again.... sometimes it works ... an then suddenly the
session is null. .. how could that be? is this an error in .NET? or where
else?
what do you think should I do, if the session is empty? reload the page
somehow and hope that after a reload the session isn't empty?

thank you for answers
 
W

William F. Robertson, Jr.

Session itself is not null. If the session times out, then the
Session["Basket"] object will be null as when a Session timesout, the next
request by the user will generate a new Session.

This is not an error in .NET, but maybe just a lack of understanding.

There are a couple different steps you could take for this to not happen.

1. Increase the Session timeout.
2. Don't cause Application Restarts. If you think your application is
restarting, I have a email that is send out on Application_Start to me, so I
can monitor restarts.
3. Place the basket into ViewState. (similiar to session) When the page
postbacks, the Basket details will be in ViewState so even if the Session
times out, the Basket will be recreated.

HTH,

bill

Alexander Widera said:
hmm ... ok .... thank you for that solution....
but I don't understand why the session is sometimes null and sometimes not
.... nothing changed ... I can click on a link to this page 100 times ...
again and again and again.... sometimes it works ... an then suddenly the
session is null. .. how could that be? is this an error in .NET? or where
else?
what do you think should I do, if the session is empty? reload the page
somehow and hope that after a reload the session isn't empty?

thank you for answers

David Lloyd said:
Alexander:

You should always check the session variable for nullity prior to reading
its value. Apparently, in some cases, your session variable is returning
null.

For example:

if (Session["Basket"] != null) {
Basket basket = (Basket) Session["Basket"];
if (basket.isEmpty)
{
...........
}
}


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or
warranties.


hi,

i have the following problem in the following code:

Basket basket = (Basket) Session["Basket"];
if (basket.isEmpty)
{
...........
}


Basket is a class which i have created myself. It has a function isEmpty
of
the type bool.

This code is in a ascx-page (usercontrol).

Now the problem. Sometimes I call a page, which includes the usercontrol
all
works, but sometimes i get the errormessage "Object reference not set to
an
instance of an object."
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Line 51: if (basket.isEmpty)



Why does this happen? Sometime it works and sometimes not?
Does somebody know this problem?


Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top