PC Review


Reply
Thread Tools Rate Thread

Arraylist issues.....

 
 
Don
Guest
Posts: n/a
 
      14th Dec 2003
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
 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      14th Dec 2003
On 2003-12-14, Don <(E-Mail Removed)> wrote:
> This is a multi-part message in MIME format.
>
>


<snip>

> '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


--
Tom Shelton
MVP [Visual Basic]
 
Reply With Quote
 
Don
Guest
Posts: n/a
 
      14th Dec 2003
Thanks Tom. I never seem to know when to use New vs. NOT using New.

--

Don
"Tom Shelton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2003-12-14, Don <(E-Mail Removed)> wrote:
> > This is a multi-part message in MIME format.
> >
> >

>
> <snip>
>
> > '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
>
>
> --
> Tom Shelton
> MVP [Visual Basic]



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      14th Dec 2003
"Don" <(E-Mail Removed)> schrieb
> Thanks Tom. I never seem to know when to use New vs. NOT using
> New.


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...fVBSpec6_1.asp


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      14th Dec 2003
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 Zingler wrote:
> "Don" <(E-Mail Removed)> schrieb
>> Thanks Tom. I never seem to know when to use New vs. NOT using
>> New.

>
> 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...fVBSpec6_1.asp


Regards - OHM# (E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread-safety: Change property of items in arraylist versus removingitems from the arraylist Curious Microsoft Dot NET 2 6th Aug 2008 12:36 PM
How to access ArrayList values inside another ArrayList? Pavel Maly Microsoft C# .NET 6 30th Oct 2006 01:46 PM
arraylist inside an arraylist for datagrid, gridview rjl Microsoft C# .NET 4 13th Apr 2006 07:32 PM
ArrayList(ICollection) constructor & overriden ArrayList.AddRange(). Sylvain Microsoft C# .NET 1 4th Jun 2005 01:19 AM
Re: ArrayList ToArray issues Jay B. Harlow [MVP - Outlook] Microsoft VB .NET 32 27th Aug 2003 10:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:42 PM.