advertisement

Search | Recent | Register

Forum => General ASP.NET => Object reference not set to an instance of an object in dynamic creation.

Jump to:

Next Oldest | Next Newest

5/19/2005 7:35:17 AM Link | Reply | Edit | Quote

Vedratna Velani

Profile Send Private Message
Location: | Joined: 3/18/2005 | Posts: 18 | Offline

Hi, everybody !

Please see the following code and error and please give me the solution. i have included all the needed file. this thing works fine in one old aspx page.

namespace WebApplication1
{
///
/// Summary description for WebForm9.
///
public class WebForm9 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton lkbtnCommonChat;
protected System.Web.UI.HtmlControls.HtmlForm form;

private void Page_Load(object sender, System.EventArgs e)
{

// Put user code to initialize the page here

form=(HtmlForm)this.FindControl("WebForm9");
Button button = new Button();
form.Controls.Add(button); //it shows error here
button.ID = "Community";
button.Text="Community";
button.Style["Alignment"]= "left";
button.BackColor=System.Drawing.Color.Transparent;
button.BorderStyle=System.Web.UI.WebControls.BorderStyle.None;
button.ForeColor=System.Drawing.Color.Blue;
button.Style["POSITION"]="relative";

}

}

}


Error:


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 27


Line 25: form=(HtmlForm)this.FindControl("WebForm9");
Line 26: Button button = new Button();
Line 27: form.Controls.Add(button);
Line 28: button.ID = "Community";
Line 29: button.Text="Community";

Source File: c:\inetpub\wwwroot\webapplication1\webform9.aspx.cs Line: 27

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
WebApplication1.WebForm9.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication1\webform9.aspx.cs:27
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573


5/19/2005 5:05:40 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

Have you stepped through the code to find out which value is null? That's certainly easier than trying to have someone else figure it out.


Jeff 'Jones' Putz
POP World Media, LLC

5/19/2005 11:04:56 PM Link | Reply | Edit | Quote

Devang

Profile Send Private Message
Location: | Joined: 5/19/2005 | Posts: 10 | Offline

Hi Jigar..
Try this....

private void Page_Load(object sender, System.EventArgs e)
{
System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
form = (HtmlForm)Page.FindControl("Form1");
Button button = new Button();
button.Text ="Test Button";
form.Controls.Add(button);
}

Here "Form1" is the name of the form, get it from HTML view of Design.


Regards..
Devang

5/20/2005 6:51:23 AM Link | Reply | Edit | Quote

Vedratna Velani

Profile Send Private Message
Location: | Joined: 3/18/2005 | Posts: 18 | Offline

hi, jeff and devang !

here it shows the value of form object is null.

i have tried ur code devang. but still the same error.

5/20/2005 6:59:00 AM Link | Reply | Edit | Quote

Devang

Profile Send Private Message
Location: | Joined: 5/19/2005 | Posts: 10 | Offline

ok then add


in your html.

Devang

5/20/2005 3:12:53 PM Link | Reply | Edit | Quote

Jeff

Profile Send Private Message Web Site AOL Instant Message ICQ Message
Location: Cleveland, OH, USA | Joined: 8/15/2000 | Posts: 813 | Offline

It makes a lot more sense to dynamically add controls to a Placeholder control.


Jeff 'Jones' Putz
POP World Media, LLC

5/21/2005 1:06:40 AM Link | Reply | Edit | Quote

Vedratna Velani

Profile Send Private Message
Location: | Joined: 3/18/2005 | Posts: 18 | Offline

its working now. Thanks to both of u.

Forum => General ASP.NET => Object reference not set to an instance of an object in dynamic creation.