NullReferenceException was Unhandled by user code

Last post 06-20-2007 6:28 PM by d4dennis@inspir3. 4 replies.

Sort Posts:

  • NullReferenceException was Unhandled by user code

    06-18-2007, 6:34 PM
    • Member
      12 point Member
    • Skip67
    • Member since 03-15-2006, 11:42 PM
    • Posts 2

    Hello.

     I am trying to create a base page class for a sort of template for all my aspx pages.

    In my BasePage.cs I would like to put in code that all my pages will use. This is what I guesse the BasePage class is suposed to do.

    I am new to asp and c# so my coding gets done with copy past and trial and error.

    The problem I am having is a piece of code that works fine when it is in the code behind of one of my pages. But when I move the code to my BasePage.cs I get a NullReferenceException was Unhandled by user code and An object  reference is not defined to an instance (this message is translated from french to english) I have pasted my code below.

    I get the error on the line that has "Header.Controls.Add(meta_Keywords);"

    Any HELP on this would really be nice

    thanx in advance.

     

     

    // BasePage.cs

    //

    // 1. Sets the session variables wMs_Language and wMs_Cuture to the users prefered IE language and culture.

    //

    // 2. Modifies the page title and (keyword, description, ) meta tags from database using pageID

    //

    // 3. TODO

     

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    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.Threading;

    using System.Globalization;

    using System.Resources;

    using System.Text.RegularExpressions;

    using System.Web.SessionState;

     

    /// <summary>

    /// Summary description for BasePage

    /// </summary>

    public class BasePage : System.Web.UI.Page

    {

    //private string _keywords;

    //private string _description;

    private string Lang;private string Meta;

     

    public BasePage()

    {

     

    //Init += new EventHandler(BasePage_Init);

    //

    // TODO: Add constructor logic here

    //

    }

    void Page_PreInit(object sender, System.EventArgs e)

    {

    if (Session["wMs_Language"] != null)

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["wMs_Language"].ToString());

    Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["wMs_Culture"].ToString());

    CreateMetaTags();

    }

    private void CreateMetaTags()

    {

    Lang = Session[
    "wMs_Language"].ToString();

    //KEYWORDS

    HtmlMeta meta_Keywords = new HtmlMeta();

    meta_Keywords.Name = "Keywords";

    meta_Keywords.Content = "Keyword-C#, Keyword-Csharp, Keyword-C-sharp, Keyword-.NET";

    Header.Controls.Add(meta_Keywords);

    //DESCRIPTION

    HtmlMeta meta_Description = new HtmlMeta();

    meta_Description.Name = "Content";

    meta_Description.Content = "Here goes a description of the content in the page.";

    Header.Controls.Add(meta_Description);

    //ROBOTS

    HtmlMeta meta_Robots = new HtmlMeta();

    meta_Robots.Name = "Robots";

    meta_Robots.Content = "INDEX,FOLLOW";

    Header.Controls.Add(meta_Robots);

    //SUBJECT

    if (Lang == "fr")

    {

    HtmlMeta meta_Subject = new HtmlMeta();

    meta_Subject.Name = "Subject";

    meta_Subject.Content = "F‚d‚ration suisse pour le developement de la raquette … neige";

    Header.Controls.Add(meta_Subject);

    }

    if (Lang == "de")

    {

    HtmlMeta meta_Subject = new HtmlMeta();

    meta_Subject.Name = "Subject";

    meta_Subject.Content = "DE: F‚d‚ration suisse pour le developement de la raquette … neige";

    Header.Controls.Add(meta_Subject);

    }

    if (Lang == "it")

    {

    HtmlMeta meta_Subject = new HtmlMeta();

    meta_Subject.Name = "Subject";

    meta_Subject.Content = "IT: F‚d‚ration suisse pour le developement de la raquette … neige";

    Header.Controls.Add(meta_Subject);

    }

    else

    {

    HtmlMeta meta_Subject = new HtmlMeta();

    meta_Subject.Name = "Subject";

    meta_Subject.Content = "Swiss Federation for the developement of snowshoe activities";

    Header.Controls.Add(meta_Subject);

    }

    }

    }

  • Re: NullReferenceException was Unhandled by user code

    06-18-2007, 8:24 PM
    • Participant
      882 point Participant
    • ravivb.net
    • Member since 01-30-2007, 7:17 AM
    • Posts 187

    Move your logic in to "Load" event handler from "PreInit" event handler.

  • Re: NullReferenceException was Unhandled by user code

    06-18-2007, 8:34 PM
    Answer

    Hi There,

    You can't reference header in preinit as header has not been initialise yet.

    That is why it throw an exception when you try to use it.

    Try this:

    protected override void OnPreLoad(EventArgs e)

    {

    base.OnPreLoad(e);

    Session["wMs_Language"] = "fr";

    if (Session["wMs_Language"] != null)

    {

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["wMs_Language"].ToString());

    Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["wMs_Culture"].ToString());

    CreateMetaTags();

    }

    }

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
  • Re: NullReferenceException was Unhandled by user code

    06-20-2007, 7:42 AM
    Answer
    • Member
      12 point Member
    • Skip67
    • Member since 03-15-2006, 11:42 PM
    • Posts 2

    Thanx a million times Dennis this worked like a charm.

    Cheers

  • Re: NullReferenceException was Unhandled by user code

    06-20-2007, 6:28 PM

    No probs, it is my pleasure ;)

    DC517
    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved.
Page 1 of 1 (5 items)