Arraylist issues.....

D

Don

Can please someone help before I go bald from pulling my hair out.

I created a usercontrol that contains a combo box and an array list. Each item in the combobox has 1 additional parameter that is associated with it. This other item is used elsewhere in my form.

Anyway--everytime I use the "Add" method of the arraylist, I get an error message:

"An unhandled exception of type 'System.NullReferenceException' occured in userControl1.dll

Additional information: Object reference not set to an instance of an object."

The code is below.

Thanks for saving my hair! ;-)

--

Don

'This is the code that CALLS the add method
'KK is just a random string I used to test whether or not the
'method was receiving the string. I performed a watch and the
'it DID show up.
cbxLocation.add("KK")

'INIFile is created in the UserControl as follows
'Public INIFile As ArrayList

'This is the add method that I created as part of my control
Public Sub add(ByVal s As String)
cbx.Items.Add(s)
INIFile.Add(s)
cbx.SelectedIndex = cbx.Items.Count - 1
End Sub

'This is the complete UserControl code (including my add method)
Imports System.Collections
Public Class myCombobox
Inherits System.Windows.Forms.UserControl
Public INIFile As ArrayList
Dim tst As String
' Dim count As Integer
Dim blankINIFile, blankName As Boolean
' Dim cbx As ComboBox
Public Sub add(ByVal s As String)
cbx.Items.Add(s)
INIFile.Add(s)
cbx.SelectedIndex = cbx.Items.Count - 1
End Sub
Dim _selectedindex As Integer
Property selectedindex() As Integer
Get()
Return cbx.SelectedIndex
End Get
Set(ByVal Value As Integer)
cbx.SelectedIndex = Value
End Set
End Property
Public Function remove(ByVal i As Integer) As Boolean
cbx.Items.Remove(i)
INIFile.Remove(i)
cbx.SelectedIndex = 0
If cbx.Items.Count = 0 Then
Return False
End If
End Function
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents cbx As System.Windows.Forms.ComboBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cbx = New System.Windows.Forms.ComboBox()
Me.SuspendLayout()
'
'cbx
'
Me.cbx.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.cbx.Name = "cbx"
Me.cbx.Size = New System.Drawing.Size(120, 21)
Me.cbx.TabIndex = 0
Me.cbx.Text = "cbx"
'
'myCombobox
'
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cbx})
Me.Name = "myCombobox"
Me.Size = New System.Drawing.Size(120, 24)
Me.ResumeLayout(False)
End Sub
#End Region
End Class
 
T

Tom Shelton

This is a multi-part message in MIME format.

'This is the code that CALLS the add method
'KK is just a random string I used to test whether or not the
'method was receiving the string. I performed a watch and the
'it DID show up.
cbxLocation.add("KK")

'INIFile is created in the UserControl as follows
'Public INIFile As ArrayList

Public INIFile As New ArrayList()

You have to create the arraylist, before you can add anything to it :)
 
O

One Handed Man [ OHM# ]

In addition, a common mistake is to try and create a new object when its not
really needed, for example.

Dim DS As New DataSet("MyDS")
Dim TBL As New DataTable("Table0")

DS.Tables.Add(TBL)

'Sometimes People do this, but it is

'not required because you are creating a new

'object an then throwing that object reference

'away when you assign it to DS.Tables("Table0")

Dim MyTab0 As New DataTable

MyTab0 = DS.Tables("Table0")

'So all you needed to have done is this. . .

'The following do the same job

'*****************************

'This is implicit, takes on the Type returned

Dim MyTab1 = DS.Tables("Table0")

'This is explicit, declares the type

Dim MyTab2 As DataTable = DS.Tables("Table0")

Regards - OHM#

*******************************************************


Armin said:
Use New whenever you want to create a new object, and the type of the
object is a reference type. Classes are reference types. Structures
are value types.

see also:
http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfVBSpec6_1.asp

Regards - OHM# (e-mail address removed)
 

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

Top