Click here to Skip to main content
16,021,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to ASP.Net and stuck with my First program itself. My label is throwing the following Exception :(
System.NullReferenceException: 'The object reference was not set to an object instance.'
)

My designer page has the Label as follows:-
protected global::System.Web.UI.WebControls.Label lblmsg;


Could someone help me, where I am going wrong.

What I have tried:

Here is my firstpage.aspx
ASP.NET
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FirstPage.aspx.cs" Inherits="WebApplicationDemo.FirstPage" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <%Response.Write("HeLLo World"); %>
             <asp:label ID="lblmsg" runat="server"/>
            <asp:Button Text="Go to Second Page" runat="server" OnClick="BtnGOTOSECOND_Click"/>
        </div>
    </form>
</body>
</html>


Here is my firstpage.aspx.cs
C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

namespace WebApplicationDemo
{
    public partial class FirstPage : System.Web.UI.Page
    {
        public FirstPage()
        {
            
            lblmsg.Text = "My First Label";
           
                   
        }
      
        protected void BtnGOTOSECOND_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("SecondPage.aspx");
        }
    }
}
Posted
Updated 29-May-19 6:45am

You're trying to access the control from the constructor. The control tree has not been created at that point.

The absolute earliest point you can access the controls is after the base FrameworkInitialize method has been invoked:
C#
protected override void FrameworkInitialize()
{
    base.FrameworkInitialize();
    
    // The control tree has now been created.
}

This typically happens just before the Init event, so that's usually the best place for this sort of code.
 
Share this answer
 
lblmsg = new Label();
lblmsg.Text = "My First Label";
 
Share this answer
 
were you able to solve this, if I initialize as the solution suggest text is not appearing actually
 
Share this answer
 
Comments
Richard Deeming 8hrs ago    
"Me too" is not a solution to anyone's question.

The accepted solution is somewhat wrong; it creates a new label, sets the text, and then throws it away.

You need to use the control created by the markup. But as the other solution explains, you can't do that before the FrameworkInitialize method has been called.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900