User control property reference gives null error

Discussion in 'ASP .Net Web Controls' started by Harry Higbie, Jul 15, 2004.

  1. Harry Higbie

    Harry Higbie Guest

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    error refers to line 34.

    This is a very simple example, and there are no syntax flags when the code is built. Is this enough info? What should I look at to fix this?

    This is happening in 2003 version of Visual Studio .NET when page and user control are both built there.

    Source Error:


    Line 32:
    Line 33: Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Line 34: Response.Write(uc1.mything)
    Line 35: End Sub
    Line 36: End Class



    Works OK, built in pre-2003 VS .NET:
    Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents btnGet As System.Web.UI.WebControls.Button
    Protected WithEvents simplecontrol1 As SimpleControl

    #Region " Web Form Designer Generated Code "

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

    End Sub

    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

    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

    Private Sub btnGet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGet.Click
    Response.Write(SimpleControl1.TextValue)
    End Sub
    End Class

    Does not work (built in VS .NET 2003):
    Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents lblResult As System.Web.UI.WebControls.Label
    Protected WithEvents uc As UserName
    Protected WithEvents uc1 As Userx

    #Region " Web Form Designer Generated Code "

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

    End Sub


    '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


    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

    Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Response.Write(uc1.mything)
    End Sub
    End Class

    This is the user control object VB code:
    Public Class Userx
    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 Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents txtName1 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

    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
    Public Property mything() As String
    Get
    Return txtName1.Text
    End Get
    Set(ByVal Value As String)
    txtName1.Text = Value
    End Set
    End Property

    End Class

    Thanks, in advance, Harry Higbie
    Harry Higbie, Jul 15, 2004
    #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. Kel Good
    Replies:
    2
    Views:
    13,118
    Kel Good
    Apr 25, 2004
  2. Replies:
    5
    Views:
    26,589
    Mike Schilling
    Mar 29, 2006
  3. Mark
    Replies:
    2
    Views:
    660
  4. Phoenix
    Replies:
    2
    Views:
    239
    Teemu Keiski
    Jun 6, 2005
  5. Tina Daniel
    Replies:
    1
    Views:
    182
    Tina Daniel
    Jul 28, 2005
Loading...

Share This Page