Timothy said:
No this is the code in global.asax :-
protected void Session_Start(Object sender, EventArgs e)
{
Session["MyShoppingBasket"] = new ShoppingBasket ();
Session["Logging"] = new Logging ();
}
And it throws an exception in the Page_load event
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
Logging log = (Logging) Session["Logging"];
log.LogPage ("default", Request.UrlReferrer.ToString ());
}
Ith throws the nullReferenceException when it try the Logging log = (Logging) Session["Logging"];
line is executed ???
Your NullReferenceException is thrown because your Session["Loggig"]
returns null. When you try to cast it using (Logging) it fails miserably.
Quick fix, rewrite as such:
Logging log = Session["Logging"] as Logging;
if (log == null)
{
Session["Logging"] = (log = new Logging());
}
then it should work.
On a wider note, it means your Session_Start was not called. Check to be
sure that the session is actually created (can be caused by cookies
problems or lack of authentication). Best way would be to put a
breakpoint on your Session["Logging"] = new Logging() line and see if,
when you request the page, it is indeed executed. My guess would be that
it is not.
As a side note, if this is supposed to be executed for all users, you
might want to put that object in Application isntead, as if it is a
utility lib, you don't need to keep it in memory.
Reading your code, i don't really understand why your Logging class
inherit from a Page? That could certainly cause problems as the Page
class is not supposed to be instanciated out of nowhere. Is there any
reason? Wouldn't it be easier and simpler to have a utility Logging
class with a static method named LogPage? That way you don't even need
to keep the object around.