+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Null exception when using user control (dll)

Null exception when using user control (dll)

8 posts, 0 answered
  1. Gus
    Gus avatar
    7 posts
    Registered:
    02 Aug 2010
    12 Aug 2010
    Link to this post
    Hi,

    I'm new to Sitefinity development and I'm trying to use a custom ASCX user control within Sitefinity 3.7.

    I need to upload it as a DLL as the ascx code behind file needs to use a custom class for its data that is taken from another DLL (from a webservice).

    When I try to add the control to sitefinity I get this error in the designer "Index was outside the bounds of the array" and when I view the page I get this error "System.NullReferenceException: Object reference not set to an instance of an object."
    Stack trace
    [NullReferenceException: Object reference not set to an instance of an object.]
       BDControls.OrderTracking.OnInit(EventArgs e) +82
       System.Web.UI.Control.InitRecursive(Control namingContainer) +333
       System.Web.UI.Control.AddedControl(Control control, Int32 index) +198
       System.Web.UI.ControlCollection.Add(Control child) +80
       Telerik.Cms.Web.PreviewPage.CreateChildControls() +1335
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
    

    At another time it told me it was the Button I was adding a method to that was null within the OnInit method.

    If I use this control (as a DLL) in another web site created through VS everything works fine and it displays and works correctly.

    Does anyone know what could be going on here?

    Thanks
  2. Gus
    Gus avatar
    7 posts
    Registered:
    02 Aug 2010
    12 Aug 2010
    Link to this post
    Also, if I'm going about this completely wrong please let me know and point me in the right direction :)
  3. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    12 Aug 2010
    Link to this post
    Hello Gus,

    As far as I see it the problem comes from BDControls.OrderTracking.OnInit of your control. There must be some value that is null and you get Object Reference exception. You can easily attach your code to VS debugger and see what actually gives you null exception. Note that in edit mode there is no page object and this could cause some issues for controls that expect this object, but this could happen only in "Edit mode"

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. Gus
    Gus avatar
    7 posts
    Registered:
    02 Aug 2010
    12 Aug 2010
    Link to this post
    Sorry, I should have stated that the null exception is caused by a button in the ASCX file, when OnInit is called and it references the button, the button is null (I've also tried OnLoad, etc). For some reason the ASP objects within the ASCX file are always null within the code behind.

    This is working correctly when I deploy it by itself outside of Sitefinity.
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    13 Aug 2010
    Link to this post
    Hello Gus,

    Can you try with this control

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestControl1.ascx.cs" Inherits="CustomControls_TestControl1" %>
     
     
    <asp:Button runat="server" Text="TestControl1" ID="TestControl1" />

    public partial class CustomControls_TestControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.TestControl1.Click += new EventHandler(TestControl1_Click);
        }
     
        void TestControl1_Click(object sender, EventArgs e)
        {
          
        }
    }


    or show same sample code that I can use to reproduce this behavior.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Gus
    Gus avatar
    7 posts
    Registered:
    02 Aug 2010
    15 Aug 2010
    Link to this post
    Hi,

    I'm having the same problem with that.

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Testing.ascx.cs" Inherits="WebApplication1.Testing" %>
    <asp:Button ID="Button1" runat="server" Text="Button" />

    and .cs file


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication1
    {
        public partial class Testing : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Button1.Click += new EventHandler(Button1_Click);
            }

            void Button1_Click(object sender, EventArgs e)
            {
                Console.WriteLine("Clicked!");
            }
        }
    }

    Error message:

    Object reference not set to an instance of an object.

    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:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       WebApplication1.Testing.Page_Load(Object sender, EventArgs e) +72
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +50
       System.Web.UI.Control.AddedControl(Control control, Int32 index) +265
       System.Web.UI.ControlCollection.Add(Control child) +80
       Telerik.Cms.Web.PreviewPage.CreateChildControls() +1332
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
    


    -------------------------
    One thing I just noticed, when I upload the control it is under the title Microsoft, not User Controls like I read it would be.

    Please note I'm uploading this as a DLL, not a ASCX control. As an ASCX it works fine.
  7. Gus
    Gus avatar
    7 posts
    Registered:
    02 Aug 2010
    16 Aug 2010
    Link to this post
    The reason I need to upload as a DLL is that I use webservices and other custom classes within the ASCX (code behind) file.

    Is there another way to do this?
  8. Gus
    Gus avatar
    7 posts
    Registered:
    02 Aug 2010
    16 Aug 2010
    Link to this post
    I've managed to get this working with an ASCX control by adding the web reference and classes directly into the website.

    There's a couple of small problems but should be easy enough to sort out.
Register for webinar
8 posts, 0 answered