Array nullexception

  • Thread starter latin & geek via DotNetMonster.com
  • Start date
L

latin & geek via DotNetMonster.com

dear all,

hi. i have a little sub routine which works fine in my dummy test program,
but when i insert it in my main program it throws errors.

this is the subroutine:

Public Sub TagListing()
'adds a tag category from the tag stack into the unique tag
collection
Try
TagStrings.Push(JustForArrayInclusion)
TagStrings.CopyTo(OldTags, moveit)
moveit = moveit + 1
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.OKOnly, errortitle)
End Try
End Sub


i get this error at the CopyTo line:

SystemArgumentNullException: Value cannot be null
Parameter name: array
at Systems.Collections.Stack.CopyTo(Array array,Int32 index)
at Catalogue.Module1.TagListing() [....] at line 137

i've declared my array like this in the same module:
'Array to hold all the unique tag categories.
'Used In: TagListing(), BtF3OldTags_Click, Form4_Load
Public OldTags As String()

moveit is correctly at zero the first time around (it's called from a FOR
loop), so i don't understand what the problem is!

can someone please help?

also, one final doubt, what's the difference between
Dim MyArray( ) as String
&
Dim MyArray as String( ) ?

thanks a lot!
 
G

Guest

latin said:
dear all,

hi. i have a little sub routine which works fine in my dummy test program,
but when i insert it in my main program it throws errors.

this is the subroutine:

Public Sub TagListing()
'adds a tag category from the tag stack into the unique tag
collection
Try
TagStrings.Push(JustForArrayInclusion)
TagStrings.CopyTo(OldTags, moveit)
moveit = moveit + 1
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.OKOnly, errortitle)
End Try
End Sub


i get this error at the CopyTo line:

SystemArgumentNullException: Value cannot be null
Parameter name: array
at Systems.Collections.Stack.CopyTo(Array array,Int32 index)
at Catalogue.Module1.TagListing() [....] at line 137

i've declared my array like this in the same module:
'Array to hold all the unique tag categories.
'Used In: TagListing(), BtF3OldTags_Click, Form4_Load
Public OldTags As String()

This declares a reference to the array, but it doesn't create the array
itself. You also need to create the array:

OldTags = New String()
moveit is correctly at zero the first time around (it's called from a FOR
loop), so i don't understand what the problem is!

can someone please help?

also, one final doubt, what's the difference between
Dim MyArray( ) as String

This is the older syntax, inherited from VB 6.
&
Dim MyArray as String( ) ?

This is the new syntax introduced in VS.NET. It's more consistent with
how .NET works, as an array in .NET are just another data type, and not
a special kind of variable as in VB 6.
 
G

Guest

Göran Andersson said:
This declares a reference to the array, but it doesn't create the array
itself. You also need to create the array:

OldTags = New String()

Oops. Of course you need to specify a size for the array:

OldTags = New String(42)
 
L

latin & geek via DotNetMonster.com

oh, i see! thanks... you're absolutely right, it works once i do that. thanks
a lot!

Göran Andersson said:
[quoted text clipped - 5 lines]
OldTags = New String()

Oops. Of course you need to specify a size for the array:

OldTags = New String(42)
 
B

Branco Medeiros

Göran Anderssonwrote:
This declares a reference to the array, but it doesn't create the array
itself. You also need to create the array:

OldTags = New String()
<snip>

The instance will be created also if you specify the upper bound of
the array. That can be -1 (yep, you read it right) to specify an empty
(but valid) array:

Dim OldTags As String(-1)

This is the older syntax, inherited from VB 6.

This is the new syntax introduced in VS.NET. It's more consistent with
how .NET works, as an array in .NET are just another data type, and not
a special kind of variable as in VB 6.
<snip>

Nope, they represent valid syntax both in VB 6 *and* VB.Net. There
are folks who prefer their array indicator in the variable name
(because the *variable* is the array, not the String). The syntax you
suggest was born (actually in VB 5, if I recall correctly) to allow
the declaration of functions returning arrays (which the other syntax
can't express).

The OP doesn't specify the VB.Net version being used, but it seems
VB2005 introduces the following syntax:

Dim MyArray(0 To UpperBound) As SomeType

Which is regarded more readable than Dim MyArray(UpperBound).

Just a bit o trivia thrown in...

Regards,

Branco.
 
L

latin & geek via DotNetMonster.com

wow. i didnt know this dot net business had a history and tradition thing
happening!! :))

thanks a lot. the (-1) declaration may be just what i need for another part
of the code - am off to try it.

gracias.
 

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