I work on an ASP.NET 1.1 portal website and am getting the following error:
Object reference not set to an instance of an object.
The stack trace for the error is as follows:
[NullReferenceException: Object reference not set to an instance of an object.] WiscNet.Portal.members.can.FetchCans() in c:\Documents and Settings\tveedahl\Desktop\Checkout\src\WiscNet.Portal\members\can.ascx.cs:48 WiscNet.Portal.members.can.InitializeComponent() in c:\Documents and Settings\tveedahl\Desktop\Checkout\src\WiscNet.Portal\members\can.ascx.cs:31 WiscNet.Portal.Business.PortalControl.OnInit(EventArgs e) in c:\Documents and Settings\tveedahl\Desktop\Checkout\src\WiscNet.Portal.Business\Controls\PortalControl.cs:37 System.Web.UI.Control.InitRecursive(Control namingContainer) +241 System.Web.UI.Control.InitRecursive(Control namingContainer) +179 System.Web.UI.Control.InitRecursive(Control namingContainer) +179 System.Web.UI.Page.ProcessRequestMain() +195
The source code for the can.ascx.cs file is as follows:
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 = "Select a Community Area Network";
//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 = "SiteCode";
this.EntityType = typeof(SiteCANS);
//this.BoundEntity = memberFacade.GetCANS( Request[ "id" ] );
//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));
}
}
}
If anyone can help me debug this error, I would greatly appreciate it. Thanks.
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:
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 (you don't need it both places).
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() into the PageLoad event for the page.
That way you know that all the controls have completed their initialization (and been assigned IDs, etc.).
Hope this helps!
Jon
InitializeComponent
Hope this helps!
Jon
If I was able to help, please mark this post as Answer.
tveedahl
Member
30 Points
27 Posts
Object reference not set to instance of an object error
Jan 08, 2009 01:38 PM|LINK
I work on an ASP.NET 1.1 portal website and am getting the following error:
Object reference not set to an instance of an object.
The stack trace for the error is as follows:
staron
Member
10 Points
17 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 01:56 PM|LINK
ComAreaNet is not initialized as far as I can tell.
jackyang
Contributor
5816 Points
782 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 01:58 PM|LINK
DropDownList ComAreaNet need to be instantiated first.
.NET Developer
ASP.NET/jQuery Spell Checker
tveedahl
Member
30 Points
27 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 02:42 PM|LINK
isn't that what this line is doing?
jackyang
Contributor
5816 Points
782 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 02:49 PM|LINK
This line merely declares a variable. If controls are declared in the code behind scenario, they must be instantiated first.
If you don't like code behind approach, you can simply add the <asp:DropDownList runat="server" ID="ComAreaNet" /> on the page markup.
.NET Developer
ASP.NET/jQuery Spell Checker
JByrd2007
Member
447 Points
93 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 02:50 PM|LINK
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:
Jon
If I was able to help, please mark this post as Answer.
tveedahl
Member
30 Points
27 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 02:59 PM|LINK
Here's my markup code:
<%@ Control Language="c#" Codebehind="can.ascx.cs" AutoEventWireup="false" Inherits="WiscNet.Portal.members.can" %> <%@ Register TagPrefix="wpc" NameSpace="WiscNet.Portal.Controls" Assembly="WiscNet.Portal.Controls" %> <%@ Import Namespace="WiscNet.Database.Model" %> <%--<asp:label id="someLabel" runat="server" />--%> <wpc:DataGrid id="Table" Runat="server" DataKeyField="Id" style="width: 415px;" GridLines="Both"> <Header Text="Community Area Networks" /> <columns> <%--Column 1--%> <asp:TemplateColumn HeaderText="CANS"> <ItemTemplate> <asp:DropDownList id="ComAreaNet" CssClass="formItem" Runat="server" /> </ItemTemplate> </asp:TemplateColumn> <%--Column 2--%> <asp:templatecolumn ItemStyle-Width="40px" HeaderText="Edit" FooterStyle-HorizontalAlign="left"> <ItemTemplate> <wpc:ImageButton id="Edit" runat="server" ImageUrl="~/images/table/edit.png" CommandName="Edit" AlternateText="Edit"/> <wpc:ImageButton id="Delete" runat="server" ImageUrl="~/images/table/delete.png" CommandName="Delete" AlternateText="Delete" /> </ItemTemplate> <EditItemTemplate> <wpc:ImageButton id="Update" runat="server" ImageUrl="~/images/table/update.png" CommandName="Update" AlternateText="Update" /> <wpc:ImageButton id="Cancel" runat="server" ImageUrl="~/images/table/cancel.png" CommandName="Cancel" AlternateText="Cancel" /> </EditItemTemplate> <FooterTemplate> <wpc:ImageButton id="Add" runat="server" ImageUrl="~/images/table/add.png" CommandName="Add" AlternateText="Add" /> </FooterTemplate> </asp:templatecolumn> </columns> </wpc:DataGrid>JByrd2007
Member
447 Points
93 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 03:07 PM|LINK
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 (you don't need it both places).
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() into the PageLoad event for the page. That way you know that all the controls have completed their initialization (and been assigned IDs, etc.).
Hope this helps!
Jon
InitializeComponent
Jon
If I was able to help, please mark this post as Answer.
tveedahl
Member
30 Points
27 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 03:30 PM|LINK
your previous post eliminated the "Object reference not set to instance of an object error"...
However, now my custom control (can.ascx)) isn't rendering on the page where I'm calling it...
<%@ Page language="c#" Codebehind="edit.aspx.cs" AutoEventWireup="false" Inherits="WiscNet.Portal.members.edit" %> <%@ Register TagPrefix="wpc" NameSpace="WiscNet.Portal.Controls" Assembly="WiscNet.Portal.Controls" %> <%@ Register TagPrefix="vam" Namespace="PeterBlum.VAM" Assembly="PeterBlum.VAM" %> <%@ Register TagPrefix="wpc" TagName="header" src="../Header.ascx" %> <%@ Register TagPrefix="wpc" TagName="footer" src="../Footer.ascx" %> <%@ Register TagPrefix="wpc" TagName="domains" src="domains.ascx" %> <%@ Register TagPrefix="wpc" TagName="can" src="can.ascx" %> <wpc:header id="Header" runat="server" /> <form id="form" runat="server"> <div class="form" style="width: 400px"> <div class="felement"> <div class="flabel">Name (SiteCode)</div> <asp:textbox id="SiteCode" Runat="server" /> <vam:RequiredTextValidator id="ValSiteCode" Runat="server" Group="Member" ControlIDToEvaluate="SiteCode" ErrorMessage="SiteCode required"/> <vam:TextLengthValidator id="ValSiteCodeLength" runat="server" ControlIDToEvaluate="SiteCode" ErrorMessage="SiteCode max 25 characters" Group="Member" Maximum="25" /> </div> <div class="felement"> <div class="flabel">Institution</div> <asp:textbox id="Institution" CssClass="formItem" Runat="server" /> <vam:RequiredTextValidator id="ValInstitutionName" Runat="server" Group="Member" ControlIDToEvaluate="Institution" ErrorMessage="Institution required" /> <vam:TextLengthValidator id="ValInstitutionLength" runat="server" ControlIDToEvaluate="Institution" ErrorMessage="Institution max 25 characters" Group="Member" Maximum="50" /> </div> <div class="felement"> <div class="flabel">Type</div> <asp:dropdownlist id="MemberType" CssClass="formItem" Runat="server" /> <vam:MultiConditionValidator id="ValMemberType" runat="server" Operator="AND" ErrorMessage="Valid member type required" Group="Member"> <conditions> <vam:RequiredTextCondition ControlIDToEvaluate="MemberType" /> <vam:RequiredListCondition ControlIDToEvaluate="MemberType" UnassignedIndex="0" /> </conditions> </vam:MultiConditionValidator> </div> <div class="felement"> <div class="flabel">Group</div> <asp:dropdownlist id="MemberGroup" CssClass="formItem" Runat="server" /> <vam:RequiredTextValidator id="ValMemberGroup" runat="server" ControlIDToEvaluate="MemberGroup" ErrorMessage="Valid member group reuired." Group="Member" /> </div> <div class="felement"> <div class="flabel">Latitude</div> <asp:textbox id="Latitude" Readonly="false" Runat="server" /> <vam:MultiConditionValidator id="ValLatitude" runat="server" Operator="AND" Group="Member" ErrorMessage="Valid latitude required."> <conditions> <vam:RequiredTextCondition ControlIDToEvaluate="Latitude" /> <vam:DataTypeCheckCondition ControlIDToEvaluate="Latitude" runat="server" DataType="Double" /> </conditions> </vam:MultiConditionValidator> </div> <div class="felement"> <div class="flabel">Longitude</div> <asp:textbox id="Longitude" Readonly="false" Runat="server" /> <vam:MultiConditionValidator id="ValLongitude" runat="server" Operator="AND" Group="Member" ErrorMessage="Valid Longitude required"> <conditions> <vam:RequiredTextCondition ControlIDToEvaluate="Longitude" /> <vam:DataTypeCheckCondition ControlIDToEvaluate="Longitude" runat="server" DataType="Double" /> </conditions> </vam:MultiConditionValidator> </div> <div class="felement"> <div class="flabel">Elevation</div> <asp:textbox id="Elevation" Readonly="false" Runat="server" /> <vam:MultiConditionValidator id="ValElevation" runat="server" Operator="AND" Group="Member" ErrorMessage="Valid elevation required"> <conditions> <vam:RequiredTextCondition ControlIDToEvaluate="Elevation" /> <vam:DataTypeCheckCondition ControlIDToEvaluate="Elevation" runat="server" DataType="Double" /> </conditions> </vam:MultiConditionValidator> </div> <div class="felement"> <div class="flabel">PrimeURL</div> <asp:textbox id="primeURL" CssClass="formItem" Runat="server" /> </div> <div class="felement"> <div class="flabel">Taxable</div> <asp:checkbox id="Taxable" CssClass="formItem" Runat="server" /> </div> <div class="felement"> <div class="flabel">Billable</div> <asp:checkbox id="Billable" CssClass="formItem" Runat="server" /> </div> <div class="felement"> <div class="flabel">CBSCode</div> <asp:textbox id="CBSCode" CssClass="formItem" Runat="server" /> <vam:RequiredTextValidator id="ValCBSCode" Runat="server" Group="Member" ControlIDToEvaluate="CBSCode" ErrorMessage="CBS Code required" /> <vam:TextLengthValidator id="ValCBSCodeLength" runat="server" ControlIDToEvaluate="CBSCode" ErrorMessage="CBSCode max 25 characters" Group="Contact" Maximum="25" /> </div> <div class="felement"> <div class="flabel">SuperSite</div> <asp:textbox id="SuperSite" CssClass="formItem" Runat="server" /> <vam:TextLengthValidator id="ValSuperSiteLength" runat="server" ControlIDToEvaluate="SuperSite" ErrorMessage="SuperSite max 25 characters" Group="Contact" Maximum="25" /> </div> <div class="felement"> <div class="flabel">SuperSite Relation</div> <asp:textbox id="SuperSiteRelation" CssClass="formItem" Runat="server" /> <vam:TextLengthValidator id="ValSuperSiteRelationLength" runat="server" ControlIDToEvaluate="SuperSiteRelation" ErrorMessage="SuperSiteRelation max 25 characters" Group="Contact" Maximum="25" /> </div> <div class="felement"> <div class="flabel">LastUpdate</div> <asp:label id="LastUpdate" CssClass="formItem" Runat="server" /> </div> <wpc:ValidationSummary id="ValSum" Runat="server" Group="Member" /> <div class="controls"> <wpc:addbutton id="Add" runat="server" Group="Member"/> <wpc:savebutton id="Save" runat="server" Group="Member"/> <wpc:deletebutton id="Delete" runat="server" Group="Member" PopupText="Are you sure you want to delete this Member and all records associated with it?" /> <wpc:cancelbutton id="Cancel" runat="server" Group="Member"/> </div> </div> <wpc:domains id="MemberDomains" runat="server" style="margin-top: 10px;" /> <%--user control for adding CAN info--%> <wpc:can id="ComAreaNet" runat="server" style="margin-top: 10px;" /> </form> <wpc:footer id="Footer" runat="server" />JByrd2007
Member
447 Points
93 Posts
Re: Object reference not set to instance of an object error
Jan 08, 2009 03:42 PM|LINK
Are you sure you have data present for the control to display, maybe that's why you're not seeing anything?
Hope this helps,
Jon
Jon
If I was able to help, please mark this post as Answer.