Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Has anyone ever seen this error?

Status
Not open for further replies.

NatGreen

Programmer
Apr 19, 2002
90
US
Error: Object reference not set to an instance of an object

I am trying to set the datasource of a listbox on a form to the arraylist generated from a custom control.

My code looks like this:

Protected withevents listbox1 as System.Web.UI.WebControls.ListBox

Public Sub ListLoad (ByVal CntArray as arraylist)

Dim CntArray As ArrayList = New ArrayList()

CntArray.Add("Sam")
CntArray.Add("Joe")
CntArray.Add("Mike")

Listbox1.DataSource = CntArray
Listbox1.DataBind()
End Sub
 
Your declaration indicates that you are passing in the CntArray, but then you dimension one, as well...

From the looks of your code, you should make the declaration look like this:

Public Sub ListLoad ()
penny1.gif
penny1.gif
 
I have tried your suggestion and I still get the same error. Any ideas?

Thanks.
NatGreen
 
Here is the cleaned up version of the aspx and ascx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
aspx:

Protected WithEvents Listbox1 As System.Web.UI.WebControls.ListBox

Public Sub ListLoad(ByVal TheNewArray As ArrayList)
If TheNewArray.Item(0) <> &quot;&quot; Then
Listbox1.DataSource = TheNewArray
Listbox1.DataBind()
End If
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ascx:

Protected WithEvents DDLUsers As System.Web.UI.WebControls.DropDownList

Public WithEvents newform As WebApplication4.Form2

Public Sub DDLUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DDLUsers.SelectedIndexChanged

Dim NewArrayList As New ArrayList()

NewArrayList.Add(&quot;Sam&quot;)
NewArrayList.Add(&quot;Joe&quot;)
NewArrayList.Add(&quot;Mike&quot;)

newform.ListLoad(NewArrayList)
End Sub
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The error occurs when it tries to set the datasource = to the array.

Any ideas?
 
I have found my error. I needed to but the databinding in the page load event of the form. Everything seemed to work okay after that.

Regards,
NatGreen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top