"Object reference not set to an instance of an object" during InitializeComponent()???

K

kw

I've spent all week trying various workarounds and keep coming back this
bug, but I just don't understand what the problem is.



An ASPX defines a control:



<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" %>

<html><body ><form id=Form1 method=post runat="server">
<iewc:treeview id=TreeViewControl runat="server" AutoPostBack="True"
SelectExpands="True">
<iewc:TreeNode Text="Jobs" ID="Jobs"></iewc:TreeNode>
..etc.

Fine so far, right?



The codebehind .cs has this line:



protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl;



And on loading the aspx:



private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

//CRASH on next line [NULLREFERENCEEXCEPTION], TreeViewControl is
<undefined>

this.TreeViewControl.SelectedIndexChange += new
Microsoft.Web.UI.WebControls.SelectEventHandler(this.TreeViewControl_Selecte
dIndexChange);



Will someone PLEASE explain what is going wrong? This seems pretty
straightforward but it just doesn't work!



Thanks a zillion!



Dan
 
S

S. Justin Gengo

Dan,

You've created an empty object holder:

protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl;

But you haven't created the object and placed it in that holder:

TreeViewControl = New Microsoft.Web.UI.WebControls.TreeView;

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
K

kw

OMG, it worked. What confused me is that the designer puts lines like this
in:
protected System.Web.UI.WebControls.Label MyLabel;
and yet these controls are able to be accessed in the Page_Load!

I don't understand why that is, but hey, it works:
protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl=new
Microsoft.Web.UI.WebControls.TreeView();


S. Justin Gengo said:
Dan,

You've created an empty object holder:

protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl;

But you haven't created the object and placed it in that holder:

TreeViewControl = New Microsoft.Web.UI.WebControls.TreeView;

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
kw said:
I've spent all week trying various workarounds and keep coming back this
bug, but I just don't understand what the problem is.



An ASPX defines a control:



<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" %>

<html><body ><form id=Form1 method=post runat="server">
<iewc:treeview id=TreeViewControl runat="server" AutoPostBack="True"
SelectExpands="True">
<iewc:TreeNode Text="Jobs" ID="Jobs"></iewc:TreeNode>
.etc.

Fine so far, right?



The codebehind .cs has this line:



protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl;



And on loading the aspx:



private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

//CRASH on next line [NULLREFERENCEEXCEPTION], TreeViewControl is
<undefined>

this.TreeViewControl.SelectedIndexChange += new
Microsoft.Web.UI.WebControls.SelectEventHandler(this.TreeViewControl_Selecte
dIndexChange);



Will someone PLEASE explain what is going wrong? This seems pretty
straightforward but it just doesn't work!



Thanks a zillion!



Dan
 
S

Scott Allen

I'd double check your ASPX file, because adding
TreeViewControl=new Microsoft.Web.UI.WebControls.TreeView(); isn't
going to attach the field to the control in the ASPX.

Sometimes just a spelling mistake will cause the code behind fields to
not be properly associated with the ASPX declarations (remember case
sensitivity also).

--
Scott
http://www.OdeToCode.com

OMG, it worked. What confused me is that the designer puts lines like this
in:
protected System.Web.UI.WebControls.Label MyLabel;
and yet these controls are able to be accessed in the Page_Load!

I don't understand why that is, but hey, it works:
protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl=new
Microsoft.Web.UI.WebControls.TreeView();


S. Justin Gengo said:
Dan,

You've created an empty object holder:

protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl;

But you haven't created the object and placed it in that holder:

TreeViewControl = New Microsoft.Web.UI.WebControls.TreeView;

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
kw said:
I've spent all week trying various workarounds and keep coming back this
bug, but I just don't understand what the problem is.



An ASPX defines a control:



<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" %>

<html><body ><form id=Form1 method=post runat="server">
<iewc:treeview id=TreeViewControl runat="server" AutoPostBack="True"
SelectExpands="True">
<iewc:TreeNode Text="Jobs" ID="Jobs"></iewc:TreeNode>
.etc.

Fine so far, right?



The codebehind .cs has this line:



protected Microsoft.Web.UI.WebControls.TreeView TreeViewControl;



And on loading the aspx:



private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

//CRASH on next line [NULLREFERENCEEXCEPTION], TreeViewControl is
<undefined>

this.TreeViewControl.SelectedIndexChange += new
Microsoft.Web.UI.WebControls.SelectEventHandler(this.TreeViewControl_Selecte
dIndexChange);



Will someone PLEASE explain what is going wrong? This seems pretty
straightforward but it just doesn't work!



Thanks a zillion!



Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top