Subject: Chapter 6 User-Selected Theme Question
Posted By: jabo2099 Post Date: 8/20/2008 12:51:22 PM
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



Reply By: jabo2099 Reply Date: 8/20/2008 3:43:57 PM
Never mind.  I figured out my problem.  I was using "preferredTheme.Value != null" rather than "preferredTheme != null" in the code on pg 277.

Reply By: Imar Reply Date: 8/21/2008 3:45:53 AM
Easy to make such a mistake.... ;-)

Glad you got it working...


Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.

Go to topic 73566

Return to index page 1