Object reference not set to instance of an object errorhttp://forums.asp.net/t/1368997.aspx/1?Object+reference+not+set+to+instance+of+an+object+errorThu, 08 Jan 2009 16:07:36 -050013689972854423http://forums.asp.net/p/1368997/2854423.aspx/1?Object+reference+not+set+to+instance+of+an+object+errorObject reference not set to instance of an object error <p>I work on an ASP.NET 1.1 portal website and am getting the following error:</p> <p>Object reference not set to an instance of an object.</p> <p>The stack trace for the error is as follows:</p> <p><font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "></p> <pre>[NullReferenceException: Object reference not set to an instance of an object.]<br> WiscNet.Portal.members.can.FetchCans() in c:\Documents and Settings\tveedahl\Desktop\Checkout\src\WiscNet.Portal\members\can.ascx.cs:48<br> WiscNet.Portal.members.can.InitializeComponent() in c:\Documents and Settings\tveedahl\Desktop\Checkout\src\WiscNet.Portal\members\can.ascx.cs:31<br> WiscNet.Portal.Business.PortalControl.OnInit(EventArgs e) in c:\Documents and Settings\tveedahl\Desktop\Checkout\src\WiscNet.Portal.Business\Controls\PortalControl.cs:37<br> System.Web.UI.Control.InitRecursive(Control namingContainer) &#43;241<br> System.Web.UI.Control.InitRecursive(Control namingContainer) &#43;179<br> System.Web.UI.Control.InitRecursive(Control namingContainer) &#43;179<br> System.Web.UI.Page.ProcessRequestMain() &#43;195 <br></pre> <pre>The source code for the can.ascx.cs file is as follows:</pre> <pre><pre class="prettyprint">using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using WiscNet.Portal.Util; using WiscNet.Database.Model; using WiscNet.Portal.Business; namespace WiscNet.Portal.members { public class can : WiscNet.Portal.Business.TableControl { public static string DEFAULT_MEMBER_COMAREANET = &quot;Select a Community Area Network&quot;; //form protected System.Web.UI.WebControls.DropDownList ComAreaNet; //protected System.Web.UI.WebControls.Label someLabel; protected MemberFacade memberFacade = new MemberFacade(); //constructor protected override void InitializeComponent() { base.InitializeComponent(); FetchCans(); } //page load //table receiving data protected override void Page_Load(object sender, EventArgs e) { this.EntityKey = &quot;SiteCode&quot;; this.EntityType = typeof(SiteCANS); //this.BoundEntity = memberFacade.GetCANS( Request[ &quot;id&quot; ] ); //base.Page_Load( sender, e ); } //get community area network list public void FetchCans() { ComAreaNet.DataSource = memberFacade.GetCANS(); ComAreaNet.DataBind(); ComAreaNet.Items.Insert(0, ControlUtil.GetNullListItem(DEFAULT_MEMBER_COMAREANET)); } } }</pre><pre class="prettyprint">If anyone can help me debug this error, I would greatly appreciate it. Thanks. <br></pre></pre></font><p></p><p><span></span></p></pre> 2009-01-08T13:38:10-05:002854464http://forums.asp.net/p/1368997/2854464.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>ComAreaNet is not initialized as far as I can tell.</p> 2009-01-08T13:56:52-05:002854471http://forums.asp.net/p/1368997/2854471.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>&nbsp;DropDownList<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> ComAreaNet need to be instantiated first.<br> </font></p> 2009-01-08T13:58:50-05:002854573http://forums.asp.net/p/1368997/2854573.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>&nbsp;isn't that what this line is doing?</p> <p>&nbsp;</p> <pre class="prettyprint">protected System.Web.UI.WebControls.DropDownList ComAreaNet;</pre>&nbsp;&nbsp; 2009-01-08T14:42:12-05:002854587http://forums.asp.net/p/1368997/2854587.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>This line merely declares a variable. If controls are declared in the code behind scenario, they must be instantiated first.<br> </p> <p>If you don't like code behind approach, you can simply add the &lt;asp:DropDownList runat=&quot;server&quot; ID=&quot;ComAreaNet&quot; /&gt; on the page markup.<br> </p> 2009-01-08T14:49:14-05:002854591http://forums.asp.net/p/1368997/2854591.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>&nbsp;<pre class="prettyprint">protected System.Web.UI.WebControls.DropDownList ComAreaNet;</pre>&nbsp;&nbsp; <P mce_keep="true">This only declares the DropDownList control, but doesn't create a new one. You're doing this declaritively by adding this line to your code. If you want the the control to be created for you automatically, you can drag/drop a DropDownList object from the Toolbox onto your User Control, which will add an entry in the ascx.designer.cs file (and create it in InitializeComponent). Otherwise, you will need to initialize the control in your InitializeComponent like this:</P> <P mce_keep="true">&nbsp;<pre class="prettyprint"><SPAN class=kwd>protected override void</SPAN> InitializeComponent() { <SPAN class=kwd>base</SPAN>.InitializeComponent(); ComAreaNet = <SPAN class=kwd>new</SPAN> DropDownList(); FetchCans(); } </pre>&nbsp;</p> 2009-01-08T14:50:53-05:002854609http://forums.asp.net/p/1368997/2854609.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>Here's my markup code: <br> </p> <pre class="prettyprint">&lt;%@ Control Language=&quot;c#&quot; Codebehind=&quot;can.ascx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;WiscNet.Portal.members.can&quot; %&gt; &lt;%@ Register TagPrefix=&quot;wpc&quot; NameSpace=&quot;WiscNet.Portal.Controls&quot; Assembly=&quot;WiscNet.Portal.Controls&quot; %&gt; &lt;%@ Import Namespace=&quot;WiscNet.Database.Model&quot; %&gt; &lt;%--&lt;asp:label id=&quot;someLabel&quot; runat=&quot;server&quot; /&gt;--%&gt; &lt;wpc:DataGrid id=&quot;Table&quot; Runat=&quot;server&quot; DataKeyField=&quot;Id&quot; style=&quot;width: 415px;&quot; GridLines=&quot;Both&quot;&gt; &lt;Header Text=&quot;Community Area Networks&quot; /&gt; &lt;columns&gt; &lt;%--Column 1--%&gt; &lt;asp:TemplateColumn HeaderText=&quot;CANS&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:DropDownList id=&quot;ComAreaNet&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateColumn&gt; &lt;%--Column 2--%&gt; &lt;asp:templatecolumn ItemStyle-Width=&quot;40px&quot; HeaderText=&quot;Edit&quot; FooterStyle-HorizontalAlign=&quot;left&quot;&gt; &lt;ItemTemplate&gt; &lt;wpc:ImageButton id=&quot;Edit&quot; runat=&quot;server&quot; ImageUrl=&quot;~/images/table/edit.png&quot; CommandName=&quot;Edit&quot; AlternateText=&quot;Edit&quot;/&gt; &lt;wpc:ImageButton id=&quot;Delete&quot; runat=&quot;server&quot; ImageUrl=&quot;~/images/table/delete.png&quot; CommandName=&quot;Delete&quot; AlternateText=&quot;Delete&quot; /&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;wpc:ImageButton id=&quot;Update&quot; runat=&quot;server&quot; ImageUrl=&quot;~/images/table/update.png&quot; CommandName=&quot;Update&quot; AlternateText=&quot;Update&quot; /&gt; &lt;wpc:ImageButton id=&quot;Cancel&quot; runat=&quot;server&quot; ImageUrl=&quot;~/images/table/cancel.png&quot; CommandName=&quot;Cancel&quot; AlternateText=&quot;Cancel&quot; /&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;wpc:ImageButton id=&quot;Add&quot; runat=&quot;server&quot; ImageUrl=&quot;~/images/table/add.png&quot; CommandName=&quot;Add&quot; AlternateText=&quot;Add&quot; /&gt; &lt;/FooterTemplate&gt; &lt;/asp:templatecolumn&gt; &lt;/columns&gt; &lt;/wpc:DataGrid&gt;</pre><pre class="prettyprint">It's still not working... <br></pre>&nbsp;&nbsp; <p><br> </p> 2009-01-08T14:59:45-05:002854621http://forums.asp.net/p/1368997/2854621.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>Try opening your can.ascx.designer.cs file. Do you see an entry for ComAreaNet in here? If so, then comment out the one you have declared in your can.ascx.cs file&nbsp;(you don't need it both places). </p> <p>I think the real root of your problem here, is you shouldn't be doing anything in InitializeComponent. The controls aren't initialized completely until this method exits. I'd suggest moving your call to FetchCans() &nbsp;into the PageLoad event for the page. That way you know that all the controls have completed their initialization (and been assigned IDs, etc.).</p> <p>Hope this helps!</p> <p>Jon</p> 2009-01-08T15:07:06-05:002854667http://forums.asp.net/p/1368997/2854667.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>your previous post eliminated the &quot;Object reference not set to instance of an object error&quot;...</p> <p>However, now my custom control (can.ascx)) isn't rendering on the page where I'm calling it...</p> <pre class="prettyprint">&lt;%@ Page language=&quot;c#&quot; Codebehind=&quot;edit.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;WiscNet.Portal.members.edit&quot; %&gt; &lt;%@ Register TagPrefix=&quot;wpc&quot; NameSpace=&quot;WiscNet.Portal.Controls&quot; Assembly=&quot;WiscNet.Portal.Controls&quot; %&gt; &lt;%@ Register TagPrefix=&quot;vam&quot; Namespace=&quot;PeterBlum.VAM&quot; Assembly=&quot;PeterBlum.VAM&quot; %&gt; &lt;%@ Register TagPrefix=&quot;wpc&quot; TagName=&quot;header&quot; src=&quot;../Header.ascx&quot; %&gt; &lt;%@ Register TagPrefix=&quot;wpc&quot; TagName=&quot;footer&quot; src=&quot;../Footer.ascx&quot; %&gt; &lt;%@ Register TagPrefix=&quot;wpc&quot; TagName=&quot;domains&quot; src=&quot;domains.ascx&quot; %&gt; &lt;%@ Register TagPrefix=&quot;wpc&quot; TagName=&quot;can&quot; src=&quot;can.ascx&quot; %&gt; &lt;wpc:header id=&quot;Header&quot; runat=&quot;server&quot; /&gt; &lt;form id=&quot;form&quot; runat=&quot;server&quot;&gt; &lt;div class=&quot;form&quot; style=&quot;width: 400px&quot;&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Name (SiteCode)&lt;/div&gt; &lt;asp:textbox id=&quot;SiteCode&quot; Runat=&quot;server&quot; /&gt; &lt;vam:RequiredTextValidator id=&quot;ValSiteCode&quot; Runat=&quot;server&quot; Group=&quot;Member&quot; ControlIDToEvaluate=&quot;SiteCode&quot; ErrorMessage=&quot;SiteCode required&quot;/&gt; &lt;vam:TextLengthValidator id=&quot;ValSiteCodeLength&quot; runat=&quot;server&quot; ControlIDToEvaluate=&quot;SiteCode&quot; ErrorMessage=&quot;SiteCode max 25 characters&quot; Group=&quot;Member&quot; Maximum=&quot;25&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Institution&lt;/div&gt; &lt;asp:textbox id=&quot;Institution&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;vam:RequiredTextValidator id=&quot;ValInstitutionName&quot; Runat=&quot;server&quot; Group=&quot;Member&quot; ControlIDToEvaluate=&quot;Institution&quot; ErrorMessage=&quot;Institution required&quot; /&gt; &lt;vam:TextLengthValidator id=&quot;ValInstitutionLength&quot; runat=&quot;server&quot; ControlIDToEvaluate=&quot;Institution&quot; ErrorMessage=&quot;Institution max 25 characters&quot; Group=&quot;Member&quot; Maximum=&quot;50&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Type&lt;/div&gt; &lt;asp:dropdownlist id=&quot;MemberType&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;vam:MultiConditionValidator id=&quot;ValMemberType&quot; runat=&quot;server&quot; Operator=&quot;AND&quot; ErrorMessage=&quot;Valid member type required&quot; Group=&quot;Member&quot;&gt; &lt;conditions&gt; &lt;vam:RequiredTextCondition ControlIDToEvaluate=&quot;MemberType&quot; /&gt; &lt;vam:RequiredListCondition ControlIDToEvaluate=&quot;MemberType&quot; UnassignedIndex=&quot;0&quot; /&gt; &lt;/conditions&gt; &lt;/vam:MultiConditionValidator&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Group&lt;/div&gt; &lt;asp:dropdownlist id=&quot;MemberGroup&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;vam:RequiredTextValidator id=&quot;ValMemberGroup&quot; runat=&quot;server&quot; ControlIDToEvaluate=&quot;MemberGroup&quot; ErrorMessage=&quot;Valid member group reuired.&quot; Group=&quot;Member&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Latitude&lt;/div&gt; &lt;asp:textbox id=&quot;Latitude&quot; Readonly=&quot;false&quot; Runat=&quot;server&quot; /&gt; &lt;vam:MultiConditionValidator id=&quot;ValLatitude&quot; runat=&quot;server&quot; Operator=&quot;AND&quot; Group=&quot;Member&quot; ErrorMessage=&quot;Valid latitude required.&quot;&gt; &lt;conditions&gt; &lt;vam:RequiredTextCondition ControlIDToEvaluate=&quot;Latitude&quot; /&gt; &lt;vam:DataTypeCheckCondition ControlIDToEvaluate=&quot;Latitude&quot; runat=&quot;server&quot; DataType=&quot;Double&quot; /&gt; &lt;/conditions&gt; &lt;/vam:MultiConditionValidator&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Longitude&lt;/div&gt; &lt;asp:textbox id=&quot;Longitude&quot; Readonly=&quot;false&quot; Runat=&quot;server&quot; /&gt; &lt;vam:MultiConditionValidator id=&quot;ValLongitude&quot; runat=&quot;server&quot; Operator=&quot;AND&quot; Group=&quot;Member&quot; ErrorMessage=&quot;Valid Longitude required&quot;&gt; &lt;conditions&gt; &lt;vam:RequiredTextCondition ControlIDToEvaluate=&quot;Longitude&quot; /&gt; &lt;vam:DataTypeCheckCondition ControlIDToEvaluate=&quot;Longitude&quot; runat=&quot;server&quot; DataType=&quot;Double&quot; /&gt; &lt;/conditions&gt; &lt;/vam:MultiConditionValidator&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Elevation&lt;/div&gt; &lt;asp:textbox id=&quot;Elevation&quot; Readonly=&quot;false&quot; Runat=&quot;server&quot; /&gt; &lt;vam:MultiConditionValidator id=&quot;ValElevation&quot; runat=&quot;server&quot; Operator=&quot;AND&quot; Group=&quot;Member&quot; ErrorMessage=&quot;Valid elevation required&quot;&gt; &lt;conditions&gt; &lt;vam:RequiredTextCondition ControlIDToEvaluate=&quot;Elevation&quot; /&gt; &lt;vam:DataTypeCheckCondition ControlIDToEvaluate=&quot;Elevation&quot; runat=&quot;server&quot; DataType=&quot;Double&quot; /&gt; &lt;/conditions&gt; &lt;/vam:MultiConditionValidator&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;PrimeURL&lt;/div&gt; &lt;asp:textbox id=&quot;primeURL&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Taxable&lt;/div&gt; &lt;asp:checkbox id=&quot;Taxable&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;Billable&lt;/div&gt; &lt;asp:checkbox id=&quot;Billable&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;CBSCode&lt;/div&gt; &lt;asp:textbox id=&quot;CBSCode&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;vam:RequiredTextValidator id=&quot;ValCBSCode&quot; Runat=&quot;server&quot; Group=&quot;Member&quot; ControlIDToEvaluate=&quot;CBSCode&quot; ErrorMessage=&quot;CBS Code required&quot; /&gt; &lt;vam:TextLengthValidator id=&quot;ValCBSCodeLength&quot; runat=&quot;server&quot; ControlIDToEvaluate=&quot;CBSCode&quot; ErrorMessage=&quot;CBSCode max 25 characters&quot; Group=&quot;Contact&quot; Maximum=&quot;25&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;SuperSite&lt;/div&gt; &lt;asp:textbox id=&quot;SuperSite&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;vam:TextLengthValidator id=&quot;ValSuperSiteLength&quot; runat=&quot;server&quot; ControlIDToEvaluate=&quot;SuperSite&quot; ErrorMessage=&quot;SuperSite max 25 characters&quot; Group=&quot;Contact&quot; Maximum=&quot;25&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;SuperSite Relation&lt;/div&gt; &lt;asp:textbox id=&quot;SuperSiteRelation&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;vam:TextLengthValidator id=&quot;ValSuperSiteRelationLength&quot; runat=&quot;server&quot; ControlIDToEvaluate=&quot;SuperSiteRelation&quot; ErrorMessage=&quot;SuperSiteRelation max 25 characters&quot; Group=&quot;Contact&quot; Maximum=&quot;25&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;felement&quot;&gt; &lt;div class=&quot;flabel&quot;&gt;LastUpdate&lt;/div&gt; &lt;asp:label id=&quot;LastUpdate&quot; CssClass=&quot;formItem&quot; Runat=&quot;server&quot; /&gt; &lt;/div&gt; &lt;wpc:ValidationSummary id=&quot;ValSum&quot; Runat=&quot;server&quot; Group=&quot;Member&quot; /&gt; &lt;div class=&quot;controls&quot;&gt; &lt;wpc:addbutton id=&quot;Add&quot; runat=&quot;server&quot; Group=&quot;Member&quot;/&gt; &lt;wpc:savebutton id=&quot;Save&quot; runat=&quot;server&quot; Group=&quot;Member&quot;/&gt; &lt;wpc:deletebutton id=&quot;Delete&quot; runat=&quot;server&quot; Group=&quot;Member&quot; PopupText=&quot;Are you sure you want to delete this Member and all records associated with it?&quot; /&gt; &lt;wpc:cancelbutton id=&quot;Cancel&quot; runat=&quot;server&quot; Group=&quot;Member&quot;/&gt; &lt;/div&gt; &lt;/div&gt; &lt;wpc:domains id=&quot;MemberDomains&quot; runat=&quot;server&quot; style=&quot;margin-top: 10px;&quot; /&gt; &lt;%--user control for adding CAN info--%&gt; &lt;wpc:can id=&quot;ComAreaNet&quot; runat=&quot;server&quot; style=&quot;margin-top: 10px;&quot; /&gt; &lt;/form&gt; &lt;wpc:footer id=&quot;Footer&quot; runat=&quot;server&quot; /&gt;</pre>&nbsp;&nbsp; 2009-01-08T15:30:21-05:002854691http://forums.asp.net/p/1368997/2854691.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>Are you sure you have data present for the control to display, maybe that's why you're not seeing anything?</p> <p>Hope this helps,</p> <p>Jon</p> 2009-01-08T15:42:51-05:002854751http://forums.asp.net/p/1368997/2854751.aspx/1?Re+Object+reference+not+set+to+instance+of+an+object+errorRe: Object reference not set to instance of an object error <p>the control is pulling data from a database table via a framework known as NHibernate...</p> <p>without going into details, I'm pretty certain this isn't the problem.</p> <p>I'm wondering if there's something wrong in the ASP.NET markup code I specified above?</p> <p>particularly the line: <br> </p> <pre class="prettyprint">&lt;wpc:can id=&quot;ComAreaNet&quot; runat=&quot;server&quot; style=&quot;margin-top: 10px;&quot; /&gt;</pre>&nbsp;&nbsp; 2009-01-08T16:07:36-05:00