object ERROR

jabsasy

New member
Joined
Sep 23, 2011
Messages
1
Programming Experience
1-3
Error Message:

System.NullReferenceException: Object reference not set to an instance of an object at contact.Default.button1Clicked (System.Object sender, System.EventArgs args) [0x00017] in /home/jess/Projects/contact/Default.aspx.vb:23 at System.Web.UI.WebControls.Button.OnClick (System.EventArgs e) [0x00000] in <filename unknown>:0 at System.Web.UI.WebControls.Button.RaisePostBackEvent (System.String eventArgument) [0x00000] in <filename unknown>:0 at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent (System.String eventArgument) [0x00000] in <filename unknown>:0 at System.Web.UI.Page.RaisePostBackEvent (IPostBackEventHandler sourceControl, System.String eventArgument) [0x00000] in <filename unknown>:0 at System.Web.UI.Page.RaisePostBackEvents () [0x00000] in <filename unknown>:0 at System.Web.UI.Page.ProcessRaiseEvents () [0x00000] in <filename unknown>:0 at System.Web.UI.Page.InternalProcessRequest () [0x00000] in <filename unknown>:0 at System.Web.UI.Page.ProcessRequest (System.Web.HttpContext context) [0x00000] in <filename unknown>:0


VB Code:
Partial Public Class [Default]
Inherits System.Web.UI.Page

Public Overridable Sub button1Clicked(ByVal sender As Object, ByVal args As EventArgs)
dim firstName() as string
dim lastName() as string
dim email() as string
dim phone() as string
dim i as integer

do while i >=0
'assign variables to data inputed to textboxes
firstName(i) = txtFirstName.text
lastName(i) = txtLastName.text
email(i) = txtEmail.text
phone(i) = txtPhone.text

'display added contact information
lblName.text = firstName(i) + " " + lastName(i)
lblEmail.text = email(i)
lblPhone.text = phone(i)

loop

End Sub
End Class






default.aspx:

<html>
<head runat="server">
<title>Contact</title>
</head>
<body>
<form id="form1" runat="server">
<fieldset style="width: 250px">
<legend>Contact Information</legend>
<asp:Label id="lblFirstName" runat="server" WIDTH="75px">First Name:</asp:Label>
<asp:TextBox id="txtFirstName" runat="server" Width="155px"></asp:TextBox><br/>
<asp:Label id="lblLastName" runat="server" WIDTH="75px">Last Name:</asp:Label>
<asp:TextBox id="txtLastName" runat="server" Width="155px" ></asp:TextBox><br/>
<asp:Label id="lblEmail1"runat="server" WIDTH="75px">Email:</asp:Label>
<asp:TextBox id="txtEmail" runat="server" Width="155px"></asp:TextBox><br/>
<asp:Label id="lblPhone1" runat="server" WIDTH="75px">Phone:</asp:Label>
<asp:TextBox id="txtPhone" runat="server" Width="155px"></asp:TextBox> <br/>
<asp:Button id="button1" runat="server" Text="Add" OnClick="button1Clicked" style="float:right"/>

</fieldset>
</form>

<fieldset style="width: 250px">
<legend>Contacts Added</legend>
<asp:Label id="lblName" runat="server">
</asp:Label>
<br/>
<asp:Label id="lblEmail" runat="server">
</asp:Label>
<br/>
<asp:Label id="lblPhone" runat="server">
</asp:Label>
</fieldset>
</body>
</html>




SUGGESTIONS?
 
The problem is that you are declaring array variables without actually creating any arrays, then trying to set an element in the an array. It's like saying "this is where my egg carton is going to go" and then trying to put an egg in it. That's where the egg carton is going to go but there's no egg carton, so you can't put an egg in it. In order to create an array you must specify its size. If you don't know the size then don't use an array. Use a collection instead. The generic List(Of T) will give you array-like behaviour with the added ability to grow and shrink as required. If you really need an array then you can call ToArray on a List after you've populated it.
 
Back
Top