Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 4th, 2008, 02:22 AM
Registered User
 
Join Date: Sep 2008
Location: Phoenix, AZ, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

Reply With Quote
  #2 (permalink)  
Old September 4th, 2008, 02:29 AM
Imar's Avatar
Wrox Author
Points: 67,317, Level: 100
Points: 67,317, Level: 100 Points: 67,317, Level: 100 Points: 67,317, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,230
Thanks: 66
Thanked 1,416 Times in 1,396 Posts
Default

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.
Reply With Quote
  #3 (permalink)  
Old September 4th, 2008, 11:21 PM
Registered User
 
Join Date: Sep 2008
Location: Phoenix, AZ, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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());

    }
}

Reply With Quote
  #4 (permalink)  
Old September 4th, 2008, 11:32 PM
Registered User
 
Join Date: Sep 2008
Location: Phoenix, AZ, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #5 (permalink)  
Old September 5th, 2008, 07:19 AM
Imar's Avatar
Wrox Author
Points: 67,317, Level: 100
Points: 67,317, Level: 100 Points: 67,317, Level: 100 Points: 67,317, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,230
Thanks: 66
Thanked 1,416 Times in 1,396 Posts
Default

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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 2 - Template.master.cs rparchment BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 October 25th, 2006 10:55 PM
Class Object reference error. mjhoagland ASP.NET 1.0 and 1.1 Basics 2 February 2nd, 2006 08:28 AM
Getting Error - "Object reference not set to an in peri C# 2 December 24th, 2004 10:20 AM
Object reference Error mrideout BOOK: Beginning ASP.NET 1.0 4 August 9th, 2004 07:29 AM
Object reference error shaileshmark SQL Server 2000 2 July 22nd, 2004 03:13 PM



All times are GMT -4. The time now is 11:37 PM.


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