Error on a user control.

C

Christopher Young

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
 
V

Victor Garcia Aprea [MVP]

Hi Christopher,
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 said:
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
 
V

Victor Garcia Aprea [MVP]

Ok, if WebUserControl1 evaluates to null chances are the parser isn't doing
its magic. Quickly looking at your code again I noticied that:

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top