p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 (http://p2p.wrox.com/forumdisplay.php?f=389)
-   -   Chapter 7 Object reference error in master.cs (http://p2p.wrox.com/showthread.php?t=70399)

mikeal_a September 4th, 2008 02:22 AM

Chapter 7 Object reference error in master.cs
 
I'm on Chapter 7 (page 255 C#) and am getting the error listed below when attempting to view the default.aspx page in the browser. It started happening right when I added the "switch (Page.Theme.ToLower())" in the master page behind code. Specifically the line "TreeView1.Visible = true;" is causing the error. Any ideas? Everything was working fine up till this point.

Thanks in advance.


Server Error in '/' Application.
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 35: case "darkgrey":
Line 36: Menu1.Visible = false;
Line 37: TreeView1.Visible = true;
Line 38: break;
Line 39: default:


Source File: c:\Documents and Settings\Home\My Documents\Visual Studio 2008\WebSites\WebSite1\MasterPages\MasterDemo.mast er.cs Line: 37

[NullReferenceException: Object reference not set to an instance of an object.]
   MasterPages_MasterDemo.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\Home\My Documents\Visual Studio 2008\WebSites\WebSite1\MasterPages\MasterDemo.mast er.cs:37
   System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender, EventArgs e) +33
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436


Imar September 4th, 2008 02:29 AM

Hi Mike,

Can you post the full markup and code behind of the master page?

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.

mikeal_a September 4th, 2008 11:21 PM

Thank you for your reply. Here is both the MasterDemo.Master and MasterDemo.Master.cs

******************
MasterDemo.Master
******************
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterDemo.master.cs" Inherits="MasterPages_MasterDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div id="PageWrapper">
        <div id="Header">Header<a class="HeaderLink" href="~/" runat="server"><asp:SiteMapDataSource
                ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
            </a></div>
        <div id="MenuWrapper">
            <asp:Menu ID="Menu1" runat="server" CssClass="MainMenu"
                DataSourceID="SiteMapDataSource1" Orientation="Horizontal"
                StaticEnableDefaultPopOutImage="False">
                <StaticSelectedStyle CssClass="StaticSelectedStyle" />
                <StaticMenuItemStyle CssClass="StaticMenuItemStyle" ForeColor="Black" />
                <DynamicHoverStyle CssClass="DynamicHoverStyle" />
                <DynamicMenuItemStyle CssClass="DynamicMenuItemStyle" />
                <StaticHoverStyle CssClass="StaticHoverStyle" />
            </asp:Menu>
        </div>
        <div id="MainContent">
            <asp:ContentPlaceHolder ID="cpMainContent" runat="server">
                <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1"
                    ShowExpandCollapse="False">
                </asp:TreeView>
            </asp:ContentPlaceHolder>
        </div>
        <div id="Sidebar">
            <asp:DropDownList ID="lstPreferredTheme" runat="server" AutoPostBack="True"
                onselectedindexchanged="lstPreferredTheme_Selected IndexChanged">
                <asp:ListItem Value="Monochrome"></asp:ListItem>
                <asp:ListItem>DarkGrey</asp:ListItem>
            </asp:DropDownList>
        </div>
        <div id="Footer">Footer Goes Here</div>
    </div>
    </form>
</body>
</html>

********************
MasterDemo.Master.cs
********************
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class MasterPages_MasterDemo : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string selectedTheme = Page.Theme;
            HttpCookie preferredTheme = Request.Cookies.Get("PreferredTheme");

            if (preferredTheme != null)
            {
                selectedTheme = preferredTheme.Value;
            }
            if (lstPreferredTheme.Items.FindByValue(selectedTheme ) != null)
            {
                lstPreferredTheme.Items.FindByValue(selectedTheme) .Selected = true;
            }
        }

        switch (Page.Theme.ToLower())
        {
            case "darkgrey":
                Menu1.Visible = false;
                TreeView1.Visible = true;
                break;
            default:
                Menu1.Visible = true;
                TreeView1.Visible = false;
                break;
        }


    }
    protected void lstPreferredTheme_SelectedIndexChanged(object sender, EventArgs e)
    {
        HttpCookie preferredTheme = new HttpCookie("preferredTheme");
        preferredTheme.Expires = DateTime.Now.AddMonths(3);
        preferredTheme.Value = lstPreferredTheme.SelectedValue;
        Response.Cookies.Add(preferredTheme);
        Response.Redirect(Request.Url.ToString());

    }
}


mikeal_a September 4th, 2008 11:32 PM

Imar,
Right after posting the source code I found the problem... It was due to me putting the TreeView1 into the ContentPlaceHolder of the master page. Thanks for your quick reply! You did a fantastic job on this book. I took some very expensive technical training for ASP and tried various other ASP.NET books and didn't get much out of it. This book blows them away. Could have saved a bunch of money :)

Thanks,
Mike


Imar September 5th, 2008 07:19 AM

Great; glad it's working...
Quote:

quote:You did a fantastic job on this book. I took some very expensive technical training for ASP and tried various other ASP.NET books and didn't get much out of it. This book blows them away. Could have saved a bunch of money :)
Spread the word, spread the word. That's exactly the kind of feedback I am writing my books for .... ;)

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.


All times are GMT -4. The time now is 01:57 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.