Object Reference Error. Pls Help!

Discussion in 'ASP .Net Building Controls' started by fshort, Feb 26, 2006.

  1. fshort

    fshort Guest

    Hi,
    I'm having a major issue with one of the custom contols I've created.
    Basically, it consists of a dropdown, button and some textboxes for the
    user to enter values. When the user changes the item in the dropdown,
    it posts back to change which textboxes appear within the control (i.e.
    other textboxes will appear based on the selection in the dropdown).
    The button is used to refresh the data in a grid based on the values
    the user keyed into the textboxes in my control. I have 2 events wired
    up for my custom control - the Click event for the button and the
    SelectedIndexChanged event for the dropdown.

    The general flow of the control processing is as follows: All the child
    controls of the custom control are added dynamically at runtime. The
    data used to build these controls are retrieved from a call to a soap
    service from the Init method of the custom control. When the control
    lifecycle gets to CreateChildControls, all the child controls are
    created based on this data. This is where the dropdown, button and
    textboxes are created and events wired up.

    This control works properly 90% of the time. The problem I'm having is
    that every once in a while when the user clicks the button, the
    SelectedIndexChanged event of the dropdown fires event though it wasnt
    changed. This is the error that occurs:

    Object reference not set to an instance of an object.
    at
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
    postData)
    at System.Web.UI.Page.ProcessRequestMain()

    What makes this so difficult to solve is that I cannot reliably
    reproduce the error. Seems to come and go. I've added my code and the
    Trace output from the control execution (some code removed for
    brevity). Any help or insight into this issue would be greatly
    appreciated.

    Thanks,
    Fred

    using System;
    using System.Collections;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.ComponentModel;
    using System.Xml;
    using System.IO;
    using System.Web.Caching;
    using System.Configuration;

    namespace MyCntl.WebControls.WebControls {

    public class ViewEditor : WebControl, INamingContainer {
    private ArrayList _FilterControls = new ArrayList();

    private DropDownList _ddlViews;
    private Button _btnRefresh;
    private string _selectedViewId;
    private object _results = null;

    protected static readonly object EventClick = new object();
    protected static readonly object EventSelectedIndexChanged = new
    object();

    #region Control Properties

    [
    Category("Action"),
    Description("Raised when the refresh button is clicked.")
    ]
    public event EventHandler Refresh {
    add {
    Events.AddHandler(EventClick, value);
    }
    remove {
    Events.RemoveHandler(EventClick, value);
    }
    }

    [
    Category("Action"),
    Description("Raised when the selected view changes.")
    ]
    public event EventHandler ViewChanged {
    add {
    Events.AddHandler(EventSelectedIndexChanged, value);
    }
    remove {
    Events.RemoveHandler(EventSelectedIndexChanged, value);
    }
    }

    /// <summary>
    /// The ID of the currently seleced view.
    /// </summary>
    [
    Category("Data"),
    DefaultValue(""),
    Description("Current selected view id."),
    Browsable(false)
    ]
    public string SelectedViewId {
    get {
    this.EnsureChildControls();
    return this._ddlViews.SelectedValue;
    }
    }

    /// <summary>
    /// Unique key for storing the data provided by the soap service in
    cache
    /// </summary>
    [
    Category("Data"),
    DefaultValue(""),
    Description("Unique key for storing the data provided by the soap
    service in cache."),
    Browsable(false)
    ]
    private string ControlKey {
    get {
    return Page.GetType().FullName + this.ID;
    }
    }

    public override ControlCollection Controls {
    get {
    EnsureChildControls();
    return base.Controls;
    }
    }
    #endregion

    #region Control Events
    protected override void OnInit(EventArgs e) {
    base.OnInit (e);
    InitializeControlData(!Page.IsPostBack);
    }

    protected override void TrackViewState() {
    base.TrackViewState();
    }

    protected override void LoadViewState(object savedState_) {
    base.LoadViewState(savedState_);
    }

    protected override object SaveViewState() {
    object viewState = base.SaveViewState();
    return viewState;
    }

    protected override void OnLoad(EventArgs e) {
    base.OnLoad (e);
    }

    /// <summary>
    /// Click event delegate
    /// </summary>
    /// <param name="e"></param>
    protected virtual void OnClick(EventArgs e) {
    EventHandler handler = (EventHandler)Events[EventClick];
    if (handler != null) {
    handler(this, e);
    }
    }

    /// <summary>
    /// SelectedIndexChanged event delegate.
    /// </summary>
    /// <param name="e"></param>
    protected virtual void OnSelectedIndexChanged(EventArgs e) {
    EventHandler handler =
    (EventHandler)Events[EventSelectedIndexChanged];
    if (handler != null) {
    handler(this, e);
    }
    }
    #endregion

    #region Event Handlers
    private void _ddlViews_SelectedIndexChanged(object sender,
    System.EventArgs e) {
    InitializeControlData(true);
    this.OnSelectedIndexChanged(e);
    }

    private void _btnRefresh_Click(object sender, EventArgs e) {
    this.OnClick(e);
    }
    #endregion

    #region Control Data
    /// <summary>
    /// Gets data needed for creating the control - either from data in
    session or from a call to the service.
    /// </summary>
    /// <param name="reinitialize_"></param>
    public void InitializeControlData(bool reinitialize_) {
    try {
    _results = Page.Session["VIEW_EDITOR_DATA"];
    if (_results != null) {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml((string)_results);
    _results = doc.DocumentElement;
    }

    if (_results == null || reinitialize_) {
    //first check the viewId that was posted from the views drop down
    this._selectedViewId = Page.Request.Params[this.UniqueID + ":" +
    "userViews"];
    if (this._selectedViewId == null || this._selectedViewId.Length ==
    0) {
    //this view id comes in the url when clicking from summary to
    list
    this._selectedViewId = Page.Request.Params["viewId"];

    }

    _results = GetControlData();

    if (_results != null) {
    Page.Session.Add("VIEW_EDITOR_DATA",
    ((XmlElement)_results).OuterXml);
    }

    this.ChildControlsCreated = false;
    }
    } catch (Exception _e) {
    System.Console.Writeln("");
    }
    }

    /// <summary>
    /// Makes call to soap service to retreive control data.
    /// </summary>
    /// <returns></returns>
    private XmlElement GetControlData() {
    XmlElement results = null;

    try {
    IntializeProxyConnection();
    results = Proxy.GetViewEditorData(this.CurrentUser,
    this.SubjectName, this.RepositoryName, this._selectedViewId);
    } catch (Exception _e) {
    throw _e;
    }

    return results;
    }

    /// <summary>
    /// Sets the proxy URL and timeout based on config params.
    /// </summary>
    private void IntializeProxyConnection() {
    if (this.DataServiceURLConfigParam.Length > 0) {

    Proxy.ServiceURL =
    (string)ConfigurationSettings.AppSettings[this.DataServiceURLConfigParam];
    Proxy.ServiceTimeout =
    Int32.Parse((string)ConfigurationSettings.AppSettings[this.DataServiceTimeoutConfigParam]);
    } else {
    Proxy.ServiceURL = this.DataServiceURL;
    Proxy.ServiceTimeout = this.DataServiceTimeout;
    }
    }
    #endregion

    #region Child Control Creation
    /// <summary>
    /// Calls to the individual methods to create the child controls
    /// </summary>
    protected override void CreateChildControls() {
    Controls.Clear();
    CreateViewsDropDown();
    CreateFilterControls();
    CreateRefreshButton();

    }

    /// <summary>
    /// Creates the Views dropdown and populated based on the xml data
    retrieved from the service.
    /// Assigns an event handler for the selectedindexchanged event.
    /// </summary>
    private void CreateViewsDropDown() {
    this._ddlViews = new DropDownList();
    XmlNode _views = ((XmlElement)_results).SelectSingleNode("Views");
    if (_views != null && _views.ChildNodes.Count > 0) {
    foreach (XmlNode node in _views.ChildNodes) {
    string viewId = node.Attributes["Id"].Value;
    string viewName = node.Attributes["Name"].Value;
    ListItem view = new ListItem(viewName, viewId);
    if (node.Attributes["default"] != null) {
    view.Selected = Boolean.Parse(node.Attributes["default"].Value);
    }
    _ddlViews.Items.Add(view);
    }
    }

    _ddlViews.ID = "userViews";
    _ddlViews.AutoPostBack = true;
    _ddlViews.SelectedIndexChanged += new
    EventHandler(this._ddlViews_SelectedIndexChanged);
    _ddlViews.Attributes.Add("class", "inputtext");
    _ddlViews.EnableViewState = true;
    Controls.Add(_ddlViews);
    }

    /// <summary>
    /// Creates the Refresh button and assigns the event handler for the
    Click event.
    /// </summary>
    private void CreateRefreshButton() {
    _btnRefresh = new Button();
    _btnRefresh.ID = "btnRefresh";
    _btnRefresh.CommandName = "Refresh";
    _btnRefresh.Click += new EventHandler(_btnRefresh_Click);
    _btnRefresh.Attributes.Add("class", "viewEditorButton");
    _btnRefresh.Text = "Refresh";

    Controls.Add(_btnRefresh);
    }

    /// <summary>
    /// Calls individual methods to create the filter controls.
    /// </summary>
    private void CreateFilterControls() {

    XmlNode Filters =
    ((XmlElement)_results).SelectSingleNode("Filters");
    if (Filters != null) {
    this._FilterControls = new ArrayList();
    foreach (XmlNode qf in Filters) {
    if (qf.Attributes["controlType"] != null) {
    string controlTypeName = qf.Attributes["controlType"].Value;
    switch(controlTypeName) {
    case "DropDownInputControl":
    CreateFilterDropDownControl(qf);

    break;
    case "DateInputContol":
    CreateFilterDateControl(qf);

    break;
    default:
    CreateFilterTextboxControl(qf);

    break;
    }
    }
    }
    }

    if (this._FilterControls.Count == 0) {
    throw new Exception("No filters configured for view.");
    }
    }


    private void CreateFilterTextboxControl(XmlNode qf_) {

    FilterTextBox tbFilter = new FilterTextBox();
    this._FilterControls.Add(tbFilter);
    }


    protected override void RenderContents(HtmlTextWriter output)
    {
    //render controls here
    }
    }
    }


    Request Details
    Session Id: Request Type: POST
    Time of Request: 2/15/2006 8:26:30 PM Status Code: 200
    Request Encoding: Unicode (UTF-8) Response Encoding: Unicode (UTF-8)
    Trace Information
    Category Message From First(s) From Last(s)
    aspx.page Begin Init
    ViewEditor.OnInit() initializing control... 0.000058 0.000058
    ViewEditor.InitializeControlData() Starting data retrieval 0.000088
    0.000031
    ViewEditor.SelectedViewId SelectedViewId set to
    0xf0b00096ad7c0000010877db8fb6800c 0.000688 0.000600
    ViewEditor.SelectedViewId SelectedViewId =
    0xf0b00096ad7c0000010877db8fb6800c 0.000742 0.000054
    ViewEditor.SelectedViewId SelectedViewId set to
    0xf0b001b0928200000108b00caa00957b 0.000844 0.000102
    ViewEditor.createQuickFilterControls Starting... 0.000871 0.000027
    ViewEditor.CreateQuickFilterDropDownControl Starting 0.000924 0.000053
    ViewEditor.CreateQuickFilterDropDownControl Done 0.002875 0.001951
    ViewEditor.CreateQuickFilterDropDownControl Starting 0.002944 0.000069
    ViewEditor.CreateQuickFilterDropDownControl Done 0.012737 0.009793
    ViewEditor.createQuickFilterControls Done... 0.012803 0.000066
    ViewEditor.InitializeControlData() data retrieval complete 0.012827
    0.000024
    ViewEditor.OnInit() init done 0.012850 0.000023
    ViewEditor.TrackViewState() TrackViewState started 0.012874 0.000024
    ViewEditor.TrackViewState() TrackViewState done 0.012897 0.000023
    aspx.page End Init 0.013016 0.000119
    aspx.page Begin LoadViewState 0.013045 0.000029
    ViewEditor.LoadViewState() LoadViewState started 0.019257 0.006211
    ViewEditor.LoadViewState() LoadViewState done 0.020940 0.001683
    aspx.page End LoadViewState 0.021008 0.000068
    aspx.page Begin ProcessPostData 0.021031 0.000023
    ViewEditor.CreateChildControls Starting 0.024868 0.003837
    ViewEditor.Controls Getting controls 0.024938 0.000070
    ViewEditor.Controls Ensure child controls called. 0.024962 0.000024
    ViewEditor.CreateGQViewsDropDown Starting 0.024986 0.000024
    ViewEditor.SelectedViewId SelectedViewId set to
    0xf0b001b0928200000108b00caa00957b 0.025098 0.000112
    ViewEditor.Controls Getting controls 0.025165 0.000067
    ViewEditor.Controls Ensure child controls called. 0.025188 0.000023
    ViewEditor.CreateGQViewsDropDown Done 0.025214 0.000026
    ViewEditor.Controls Getting controls 0.025274 0.000060
    ViewEditor.Controls Ensure child controls called. 0.025298 0.000024
    ViewEditor.Controls Getting controls 0.025645 0.000348
    ViewEditor.Controls Ensure child controls called. 0.025712 0.000066
    ViewEditor.Controls Getting controls 0.025737 0.000025
    ViewEditor.Controls Ensure child controls called. 0.025769 0.000032
    ViewEditor.Controls Getting controls 0.025803 0.000034
    ViewEditor.Controls Ensure child controls called. 0.025826 0.000023
    ViewEditor.CreateRefreshButton Starting 0.025850 0.000024
    ViewEditor.Controls Getting controls 0.025879 0.000029
    ViewEditor.Controls Ensure child controls called. 0.025902 0.000023
    ViewEditor.CreateRefreshButton done 0.025927 0.000025
    ViewEditor.Controls Getting controls 0.025967 0.000040
    ViewEditor.Controls Ensure child controls called. 0.025991 0.000023
    ViewEditor.CreateChildControls Done 0.026014 0.000023
    aspx.page End ProcessPostData 0.049396 0.023382
    ViewEditor.OnLoad() Loading 0.049514 0.000118
    ViewEditor.OnLoad() load done 0.049540 0.000026
    aspx.page Begin ProcessPostData Second Try 0.049634 0.000094
    aspx.page End ProcessPostData Second Try 0.049691 0.000056
    aspx.page Begin Raise ChangedEvents 0.049715 0.000024
    ViewEditor._ddlViews_SelectedIndexChanged
    _ddlViews_SelectedIndexChanged started. 0.050883 0.001169
    ViewEditor.InitializeControlData() Starting data retrieval 0.050982
    0.000099
    ViewEditor.SelectedViewId SelectedViewId set to
    0xf0b00096ad7c0000010877db8fb6800c 0.051023 0.000041
    ViewEditor.SelectedViewId SelectedViewId =
    0xf0b00096ad7c0000010877db8fb6800c 0.051047 0.000024
    ViewEditor.GetControlData Starting... 0.051098 0.000051
    ViewEditor.SelectedViewId SelectedViewId =
    0xf0b00096ad7c0000010877db8fb6800c 0.051197 0.000099
    ViewEditor.GetControlData Done. 0.718004 0.666807
    ViewEditor.InitializeControlData() Cache entry created as
    _ASP.summary_aspxveSummaryalfred 0.720741 0.002737
    ViewEditor.SelectedViewId SelectedViewId set to
    0xf0b001b0928200000108b00caa00957b 0.720816 0.000075
    ViewEditor.createQuickFilterControls Starting... 0.720833 0.000017
    ViewEditor.CreateQuickFilterDropDownControl Starting 0.720870 0.000037
    ViewEditor.CreateQuickFilterDropDownControl Done 0.722047 0.001176
    ViewEditor.CreateQuickFilterDropDownControl Starting 0.722082 0.000035
    ViewEditor.CreateQuickFilterDropDownControl Done 0.727788 0.005706
    ViewEditor.CreateQuickFilterDropDownControl Starting 0.727839 0.000051
    ViewEditor.CreateQuickFilterDropDownControl Done 0.727928 0.000089
    ViewEditor.CreateQuickFilterDropDownControl Starting 0.727949 0.000021
    ViewEditor.CreateQuickFilterDropDownControl Done 0.728002 0.000052
    ViewEditor.CreateQuickFilterDropDownControl Starting 0.728021 0.000019
    ViewEditor.CreateQuickFilterDropDownControl Done 0.728085 0.000064
    ViewEditor.createQuickFilterControls Done... 0.728104 0.000018
    ViewEditor.InitializeControlData() data retrieval complete 0.728120
    0.000016
    ViewEditor.CreateChildControls Starting 0.728136 0.000016
    ViewEditor.Controls Getting controls 0.728150 0.000015
    ViewEditor.Controls Ensure child controls called. 0.728164 0.000014
    ViewEditor.CreateGQViewsDropDown Starting 0.728698 0.000534
    ViewEditor.SelectedViewId SelectedViewId set to
    0xf0b00096ad7c0000010877db8fb6800c 0.728787 0.000089
    ViewEditor.Controls Getting controls 0.728805 0.000018
    ViewEditor.Controls Ensure child controls called. 0.728821 0.000015
    ViewEditor.CreateGQViewsDropDown Done 0.728837 0.000016
    ViewEditor.Controls Getting controls 0.728907 0.000070
    ViewEditor.Controls Ensure child controls called. 0.728924 0.000017
    ViewEditor.Controls Getting controls 0.729163 0.000239
    ViewEditor.Controls Ensure child controls called. 0.729193 0.000030
    ViewEditor.Controls Getting controls 0.729210 0.000017
    ViewEditor.Controls Ensure child controls called. 0.729225 0.000015
    ViewEditor.Controls Getting controls 0.729240 0.000015
    ViewEditor.Controls Ensure child controls called. 0.729255 0.000015
    ViewEditor.Controls Getting controls 0.729270 0.000015
    ViewEditor.Controls Ensure child controls called. 0.729285 0.000015
    ViewEditor.Controls Getting controls 0.729301 0.000015
    ViewEditor.Controls Ensure child controls called. 0.729315 0.000015
    ViewEditor.Controls Getting controls 0.729355 0.000040
    ViewEditor.Controls Ensure child controls called. 0.729372 0.000017
    ViewEditor.CreateRefreshButton Starting 0.729388 0.000017
    ViewEditor.Controls Getting controls 0.729408 0.000019
    ViewEditor.Controls Ensure child controls called. 0.729423 0.000016
    ViewEditor.CreateRefreshButton done 0.729439 0.000015
    ViewEditor.Controls Getting controls 0.729466 0.000027
    ViewEditor.Controls Ensure child controls called. 0.729482 0.000016
    ViewEditor.CreateChildControls Done 0.729498 0.000016
    ViewEditor.OnViewChanged() OnViewChanged started. 0.729853 0.000355
    Summary.aspx.cs.GetSummaryData() getting data... 0.729917 0.000064
    ViewEditor.SelectedViewId SelectedViewId =
    0xf0b00096ad7c0000010877db8fb6800c 0.730371 0.000455
    Summary.aspx.cs.uwgSummary_InitializeLayout initializing layout...
    1.070761 0.340390
    Summary.aspx.cs.uwgSummary_InitializeLayout layout initialized.
    1.070818 0.000057
    Summary.aspx.cs.GetSummaryData() data retrieval complete. 1.070850
    0.000033
    ViewEditor.OnViewChanged() OnViewChanged completed. 1.070867 0.000017
    ViewEditor._ddlViews_SelectedIndexChanged
    _ddlViews_SelectedIndexChanged completed. 1.070882 0.000015
    aspx.page End Raise ChangedEvents 1.070897 0.000015
    aspx.page Begin Raise PostBackEvent 1.070912 0.000015
    Unhandled Execution Error
    Object reference not set to an instance of an object.
    at
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument)
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
    postData)
    at System.Web.UI.Page.ProcessRequestMain() 1.073220 0.002309
    Control Tree
    Control Id Type Render Size Bytes (including children) Viewstate Size
    Bytes (excluding children)
    fshort, Feb 26, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parthiv Joshi
    Replies:
    2
    Views:
    675
    Kalyan
    Jul 2, 2004
  2. Pedro Correia
    Replies:
    0
    Views:
    615
    Pedro Correia
    Jul 25, 2003
  3. Replies:
    1
    Views:
    468
    Mark Rae [MVP]
    Sep 20, 2007
  4. Vishwanathan Raman

    TreeView ASP.Net subtree.all is null or not an object -- Pls HELP!

    Vishwanathan Raman, Jun 20, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    169
    Harolds
    Jun 20, 2005
  5. G Klinedinst

    Need help passing arrays by reference pls.

    G Klinedinst, Feb 6, 2004, in forum: Perl Misc
    Replies:
    13
    Views:
    228
    Ben Morrow
    Feb 8, 2004
Loading...

Share This Page