Chapter 6 User-Selected Theme Question
My question refers to the section in Chapter 6 on Themes, in particular pages pages 222-228.
I've been making my way through the book, building the PlanetWrox website as I go, using the C# examples. Once I got to the part on Themes and cookies, I thought I might play around with it to see what would happen if I got rid of the cookie or let it expire. (Setting the lifespan of the cookie to 1 minute vs. 3 months for example.)
Unfortunately once the cookie expires, the site stops working. I get the following error at run time in the browser(with line 25 highlighted in red):
----------------------------
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 23: HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");
Line 24:
*Line 25: if (preferredTheme.Value != null)
Line 26: {
Line 27: Page.Theme = preferredTheme.Value;
Source File: c:\BegASPNET\Site\App_Code\BasePage.cs Line: 25
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
BasePage.Page_PreInit(Object sender, EventArgs e) in c:\BegASPNET\Site\App_Code\BasePage.cs:25
System.Web.UI.Page.OnPreInit(EventArgs e) +75
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1344
--------------------------
Any ideas on how to fix this other than taking out the code in the Page_PreInit Sub from pg 227 and recompiling? Once I do that, I can safely add the Page_PreInit code back in, and it works again. Guess I'm just wondering how to make this work if a cookie doesn't already exist or if it expired.
Thanks,
Jason
|