Error on a user control.

Discussion in 'ASP .Net Building Controls' started by Christopher Young, Oct 20, 2003.

  1. Here is my error. I have tried something similar in a
    larger application I am working on and got similar
    results. Thanks for any help in advance.

    Server Error in '/WebApplication3' Application.
    ----------------------------------------------------------
    ----------------------

    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:


    Line 39:
    Line 40:
    Line 41: Label1.Text = WebUserControl1.FirstName
    Line 42: Label2.Text = WebUserControl1.LastName
    Line 43:


    Source File: c:\inetpub\wwwroot\WebApplication3
    \WebForm1.aspx.vb Line: 41

    Stack Trace:


    [NullReferenceException: Object reference not set to an
    instance of an object.]
    WebApplication3.WebForm1.Button1_Click(Object sender,
    EventArgs e) in c:\inetpub\wwwroot\WebApplication3
    \WebForm1.aspx.vb:41
    System.Web.UI.WebControls.Button.OnClick(EventArgs e)

    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
    entHandler.RaisePostBackEvent(String eventArgument)
    System.Web.UI.Page.RaisePostBackEvent
    (IPostBackEventHandler sourceControl, String
    eventArgument)
    System.Web.UI.Page.RaisePostBackEvent
    (NameValueCollection postData)
    System.Web.UI.Page.ProcessRequestMain()




    ----------------------------------------------------------
    ----------------------
    Version Information: Microsoft .NET Framework
    Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

    Here is the code for the aspx.

    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb"
    Inherits="WebApplication3.WebForm1"%>
    <%@ Register TagPrefix="uc1" TagName="WebUserControl1"
    Src="WebUserControl1.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" content="Microsoft
    Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE"
    content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript"
    content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post"
    runat="server">
    <P>
    <uc1:WebUserControl1
    id="WebUserControl11"
    runat="server"></uc1:WebUserControl1></P>
    <P>
    <asp:Button id="Button1"
    runat="server" Text="Button"></asp:Button></P>
    <P>First Name:
    <asp:Label id="Label1"
    runat="server">Label</asp:Label></P>
    <P>Last Name:
    <asp:Label id="Label2"
    runat="server">Label</asp:Label></P>
    </form>
    </body>
    </HTML>
    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private
    Sub InitializeComponent()

    End Sub
    Protected WithEvents Button1 As
    System.Web.UI.WebControls.Button
    Protected WithEvents Label1 As
    System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As
    System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is
    required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As
    System.Object

    Private Sub Page_Init(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web
    Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Public WithEvents WebUserControl1 As
    WebApplication3.WebUserControl1

    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    Button1.Click

    'Dim cont As New WebUserControl1



    Label1.Text = WebUserControl1.FirstName
    Label2.Text = WebUserControl1.LastName

    End Sub
    End Class

    Here is the code for the ascx.

    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="WebUserControl1.ascx.vb"
    Inherits="WebApplication3.WebUserControl1"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie
    5" %>
    <P>First Name:
    <asp:TextBox id="TextBox1"
    runat="server"></asp:TextBox></P>
    <P>Last Name:
    <asp:TextBox id="TextBox2"
    runat="server"></asp:TextBox></P>

    Public MustInherit Class WebUserControl1
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private
    Sub InitializeComponent()

    End Sub
    Protected WithEvents TextBox1 As
    System.Web.UI.WebControls.TextBox
    Protected WithEvents TextBox2 As
    System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is
    required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As
    System.Object

    Private Sub Page_Init(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web
    Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Public ReadOnly Property FirstName() As String
    Get
    Return Me.TextBox1.Text
    End Get
    End Property

    Public ReadOnly Property LastName() As String
    Get
    Return Me.TextBox2.Text
    End Get
    End Property

    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    End Class
     
    Christopher Young, Oct 20, 2003
    #1
    1. Advertising

  2. Hi Christopher,

    >>>>Line 41: Label1.Text = WebUserControl1.FirstName

    Either Label1 or WebUserControl1 is null at this point, have you tried using
    a debugger to know which one is evaluating to null.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    and not by private mail.
    "Christopher Young" <> wrote in message
    news:027801c39743$b6d17850$...
    > Here is my error. I have tried something similar in a
    > larger application I am working on and got similar
    > results. Thanks for any help in advance.
    >
    > Server Error in '/WebApplication3' Application.
    > ----------------------------------------------------------
    > ----------------------
    >
    > 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:
    >
    >
    > Line 39:
    > Line 40:
    > Line 41: Label1.Text = WebUserControl1.FirstName
    > Line 42: Label2.Text = WebUserControl1.LastName
    > Line 43:
    >
    >
    > Source File: c:\inetpub\wwwroot\WebApplication3
    > \WebForm1.aspx.vb Line: 41
    >
    > Stack Trace:
    >
    >
    > [NullReferenceException: Object reference not set to an
    > instance of an object.]
    > WebApplication3.WebForm1.Button1_Click(Object sender,
    > EventArgs e) in c:\inetpub\wwwroot\WebApplication3
    > \WebForm1.aspx.vb:41
    > System.Web.UI.WebControls.Button.OnClick(EventArgs e)
    >
    > System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
    > entHandler.RaisePostBackEvent(String eventArgument)
    > System.Web.UI.Page.RaisePostBackEvent
    > (IPostBackEventHandler sourceControl, String
    > eventArgument)
    > System.Web.UI.Page.RaisePostBackEvent
    > (NameValueCollection postData)
    > System.Web.UI.Page.ProcessRequestMain()
    >
    >
    >
    >
    > ----------------------------------------------------------
    > ----------------------
    > Version Information: Microsoft .NET Framework
    > Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
    >
    > Here is the code for the aspx.
    >
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="WebForm1.aspx.vb"
    > Inherits="WebApplication3.WebForm1"%>
    > <%@ Register TagPrefix="uc1" TagName="WebUserControl1"
    > Src="WebUserControl1.ascx" %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    > Transitional//EN">
    > <HTML>
    > <HEAD>
    > <title>WebForm1</title>
    > <meta name="GENERATOR" content="Microsoft
    > Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE"
    > content="Visual Basic .NET 7.1">
    > <meta name="vs_defaultClientScript"
    > content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body>
    > <form id="Form1" method="post"
    > runat="server">
    > <P>
    > <uc1:WebUserControl1
    > id="WebUserControl11"
    > runat="server"></uc1:WebUserControl1></P>
    > <P>
    > <asp:Button id="Button1"
    > runat="server" Text="Button"></asp:Button></P>
    > <P>First Name:
    > <asp:Label id="Label1"
    > runat="server">Label</asp:Label></P>
    > <P>Last Name:
    > <asp:Label id="Label2"
    > runat="server">Label</asp:Label></P>
    > </form>
    > </body>
    > </HTML>
    > Public Class WebForm1
    > Inherits System.Web.UI.Page
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private
    > Sub InitializeComponent()
    >
    > End Sub
    > Protected WithEvents Button1 As
    > System.Web.UI.WebControls.Button
    > Protected WithEvents Label1 As
    > System.Web.UI.WebControls.Label
    > Protected WithEvents Label2 As
    > System.Web.UI.WebControls.Label
    >
    > 'NOTE: The following placeholder declaration is
    > required by the Web Form Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As
    > System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web
    > Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Public WithEvents WebUserControl1 As
    > WebApplication3.WebUserControl1
    >
    > Private Sub Page_Load(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles MyBase.Load
    >
    > End Sub
    >
    > Private Sub Button1_Click(ByVal sender As
    > System.Object, ByVal e As System.EventArgs) Handles
    > Button1.Click
    >
    > 'Dim cont As New WebUserControl1
    >
    >
    >
    > Label1.Text = WebUserControl1.FirstName
    > Label2.Text = WebUserControl1.LastName
    >
    > End Sub
    > End Class
    >
    > Here is the code for the ascx.
    >
    > <%@ Control Language="vb" AutoEventWireup="false"
    > Codebehind="WebUserControl1.ascx.vb"
    > Inherits="WebApplication3.WebUserControl1"
    > TargetSchema="http://schemas.microsoft.com/intellisense/ie
    > 5" %>
    > <P>First Name:
    > <asp:TextBox id="TextBox1"
    > runat="server"></asp:TextBox></P>
    > <P>Last Name:
    > <asp:TextBox id="TextBox2"
    > runat="server"></asp:TextBox></P>
    >
    > Public MustInherit Class WebUserControl1
    > Inherits System.Web.UI.UserControl
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private
    > Sub InitializeComponent()
    >
    > End Sub
    > Protected WithEvents TextBox1 As
    > System.Web.UI.WebControls.TextBox
    > Protected WithEvents TextBox2 As
    > System.Web.UI.WebControls.TextBox
    >
    > 'NOTE: The following placeholder declaration is
    > required by the Web Form Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As
    > System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web
    > Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Public ReadOnly Property FirstName() As String
    > Get
    > Return Me.TextBox1.Text
    > End Get
    > End Property
    >
    > Public ReadOnly Property LastName() As String
    > Get
    > Return Me.TextBox2.Text
    > End Get
    > End Property
    >
    > Private Sub Page_Load(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > End Class
     
    Victor Garcia Aprea [MVP], Oct 23, 2003
    #2
    1. Advertising

  3. It is the WebUserControl1 that is throwing the exception.



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Christopher Young, Oct 23, 2003
    #3
  4. Ok, if WebUserControl1 evaluates to null chances are the parser isn't doing
    its magic. Quickly looking at your code again I noticied that:

    >>> Public WithEvents WebUserControl1 As WebApplication3.WebUserControl1


    >>> <uc1:WebUserControl1 id="WebUserControl11"> runat="server">


    The value for the ID attribute of the <uc1:WebUserControl1> tag is not
    matching the name of your member variable, you need both to match for the
    parser to do its magic, so try changing the previous line to:

    <uc1:WebUserControl1 id="WebUserControl1"> runat="server">

    and it should working,

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    and not by private mail.
    "Christopher Young" <> wrote in message
    news:...
    > It is the WebUserControl1 that is throwing the exception.
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Victor Garcia Aprea [MVP], Oct 23, 2003
    #4
    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. Tom Rowton
    Replies:
    2
    Views:
    674
    Tom Rowton
    Aug 1, 2003
  2. vatech1993
    Replies:
    4
    Views:
    5,697
    =?Utf-8?B?U8Opcmdpbw==?=
    Dec 11, 2004
  3. Replies:
    0
    Views:
    439
  4. Mad Scientist Jr
    Replies:
    0
    Views:
    650
    Mad Scientist Jr
    Mar 22, 2006
  5. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    562
    Jonathan Wood
    Feb 2, 2010
Loading...

Share This Page