Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Another Object reference not set to an instance of an object

Another Object reference not set to an instance of an object

Scheduled Pinned Locked Moved ASP.NET
helpquestion
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rturner003
    wrote on last edited by
    #1

    Code in page_load is if (!IsPostBack) { Label x = new Label(); x.ID="lblQuestionX"; x.Text="Robert"; plhQuestions.Controls.Add(x); } pressing submit button private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { Label lblTemp = new Label(); lblTemp = (Label)plhQuestions.FindControl("lblQuestionX"); string strNumber = lblTemp.Text; last line generates error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. What stupid thing am I doing wrong this time? cheers Robert T Turner South Gloucestershire Council

    A 1 Reply Last reply
    0
    • R rturner003

      Code in page_load is if (!IsPostBack) { Label x = new Label(); x.ID="lblQuestionX"; x.Text="Robert"; plhQuestions.Controls.Add(x); } pressing submit button private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { Label lblTemp = new Label(); lblTemp = (Label)plhQuestions.FindControl("lblQuestionX"); string strNumber = lblTemp.Text; last line generates error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. What stupid thing am I doing wrong this time? cheers Robert T Turner South Gloucestershire Council

      A Offline
      A Offline
      Andrew Quinn AUS
      wrote on last edited by
      #2

      Robert, The problem is that you dont re-create the Label on postback. Remember the page no longer exists internally when the page is sent to the client, on postback, ASP.NET recreates the page (object) and 'sets' values defined in the viewstate. So in your example when Button1_Click is executed, "lblQuestionX" no longer exists. To correct the problem, try the following in your Page_Load:

      Label x = new Label();
      x.ID = "lblQuestionX";
      plhQuestions.Controls.Add(x);

      if (!IsPostBack)
      {
      // only set the initial value when delivering fresh page
      x.Text = "Robert";
      }

      Also, in your OnClick handler you don't need to create an instance of Label. FindControl will return you a reference to the object. Hope this helps, Andy

      R 1 Reply Last reply
      0
      • A Andrew Quinn AUS

        Robert, The problem is that you dont re-create the Label on postback. Remember the page no longer exists internally when the page is sent to the client, on postback, ASP.NET recreates the page (object) and 'sets' values defined in the viewstate. So in your example when Button1_Click is executed, "lblQuestionX" no longer exists. To correct the problem, try the following in your Page_Load:

        Label x = new Label();
        x.ID = "lblQuestionX";
        plhQuestions.Controls.Add(x);

        if (!IsPostBack)
        {
        // only set the initial value when delivering fresh page
        x.Text = "Robert";
        }

        Also, in your OnClick handler you don't need to create an instance of Label. FindControl will return you a reference to the object. Hope this helps, Andy

        R Offline
        R Offline
        rturner003
        wrote on last edited by
        #3

        Thanks very much. I knew it would be something stupid. I very new to .net and I flitting back and from between asp and asp.net. You help is very much appreciated Robert T Turner South Gloucestershire Council

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups