Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 16th, 2005, 11:53 PM
Registered User
 
Join Date: Oct 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default NullReferenceException error

I was following along with one of wrox books “Beginning C# Web Applications with Visual Studio.NET” building the Friends Reunion application. I made it to chapter 3, but I keep getting an error each time I try to compile the default.aspx page. I keep getting the error:

“System.NullReferenceException: Object reference not set to an instance of an object.”

I copied the code just as illustrated in the book. Can someone help me. I’ve included the code below.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;

using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace FriendsReunion
{
    /// <summary>
    /// Summary description for _Default.
    /// </summary>
    public class _Default : System.Web.UI.Page
    {
        protected FriendsReunion.SubHeader ccSubheader;
        protected System.Web.UI.WebControls.PlaceHolder phNav;

        private void Page_Load(object sender, System.EventArgs e)
        {
            Table tb = new Table();
            TableRow row;
            TableCell cell;
            Image img;
            HyperLink lnk;

            if (Context.User.Identity.IsAuthenticated)
            {
                // Create a new blank table row
                row = new TableRow();

                // Set up the News image
                img = new Image();
                img.ImageUrl = "Images/winbook.gif";
                img.ImageAlign = ImageAlign.Middle;
                img.Width = new Unit(24, UnitType.Pixel);
                img.Height = new Unit(24, UnitType.Pixel);

                // Create a cell and add the image
                cell = new TableCell();
                cell.Controls.Add(img);

                // Add the new cell to the row
                row.Cells.Add(cell);

                // Set up the News link
                lnk = new HyperLink();
                lnk.Text = "News";
                lnk.NavigateUrl = "News.aspx";

                // Create the cell and add the link
                cell = new TableCell();
                cell.Controls.Add(lnk);

                // Add the new cell to the row
                row.Cells.Add(cell);

                // Add the row to the table
                tb.Rows.Add(row);
            }
            else
            {
                // Code for unauthenticated users here...
            }

            // Finally, add the table to the placeholder
            phNav.Controls.Add(tb);
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}


Bon





Similar Threads
Thread Thread Starter Forum Replies Last Post
System.NullReferenceException JayLou BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 April 6th, 2007 12:55 PM
System.NullReferenceException m_rajib74 C# 0 March 5th, 2007 04:50 AM
System.NullReferenceException lwheless BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 24th, 2006 05:47 AM
NullReferenceException Kalashnikov General .NET 1 November 26th, 2004 01:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.