A little help with an array & 'NullReferenceException was unhandled' ??

D

ducky801

Hi all. When i run the code below i get a 'NullReferenceException was
unhandled' error. I am using VB 2005 Express.

I'm confused about this because, I have declared my array and i'm
trying to put things in it, so shouldn't it BE NULL until i start to
put things in it? I'm kind of new to this so hoepfully i'm just
overlooking something easy. I know my code is actually talking to the
DB (or dataset rather) because the messageboxes work. if i pull out
the buggy line, it will messagebox every login id in the table. Any
help that anybody can offer is greatly appreciated:


Module DynamicACSAuto
Public ArrLoginIDs() As Integer 'arra. holds phone logins. used
by createTBL()
___________________________________________________________________________

Sub GetLoginIDs()
'this procedure makes the program query the empdata table in
the empdb. it is used to put
'the login ID of each agent into an array, which will then be
iterated through to create
'the dynamic TBL reports in the CreateTBL routine
Dim RowCount As Integer 'holds number of records in the dataset
Dim Count As Integer
RowCount = Form1.EmpDBDataSet.Tables("Empdata").Rows.Count
Count = 1

Do Until Count = RowCount

MsgBox(Form1.EmpDBDataSet.Tables("empdata").Rows(Count).Item("phonelogin"))
'CODE BUGS AT NEXT LINE. ##########################
ArrLoginIDs(Count - 1) =
Form1.EmpDBDataSet.Tables("empdata").Rows
'BUGGY LINE ABOVE ################################
(Count).Item("phonelogin")
Count = Count + 1
Loop
MsgBox(ArrLoginIDs)
MsgBox(Count)

End Sub
 
D

ducky801

ducky801 said:
Hi all. When i run the code below i get a 'NullReferenceException was
unhandled' error. I am using VB 2005 Express.

I'm confused about this because, I have declared my array and i'm
trying to put things in it, so shouldn't it BE NULL until i start to
put things in it? I'm kind of new to this so hoepfully i'm just
overlooking something easy. I know my code is actually talking to the
DB (or dataset rather) because the messageboxes work. if i pull out
the buggy line, it will messagebox every login id in the table. Any
help that anybody can offer is greatly appreciated:


Module DynamicACSAuto
Public ArrLoginIDs() As Integer 'arra. holds phone logins. used
by createTBL()
___________________________________________________________________________

Sub GetLoginIDs()
'this procedure makes the program query the empdata table in
the empdb. it is used to put
'the login ID of each agent into an array, which will then be
iterated through to create
'the dynamic TBL reports in the CreateTBL routine
Dim RowCount As Integer 'holds number of records in the dataset
Dim Count As Integer
RowCount = Form1.EmpDBDataSet.Tables("Empdata").Rows.Count
Count = 1

Do Until Count = RowCount

MsgBox(Form1.EmpDBDataSet.Tables("empdata").Rows(Count).Item("phonelogin"))
'CODE BUGS AT NEXT LINE. ##########################
ArrLoginIDs(Count - 1) =
Form1.EmpDBDataSet.Tables("empdata").Rows
'BUGGY LINE ABOVE ################################
(Count).Item("phonelogin")
Count = Count + 1
Loop
MsgBox(ArrLoginIDs)
MsgBox(Count)

End Sub

Something got trimmed in the post above. the buggy line reads:
(without wrapping)

ArrLoginIDs(Count - 1) =
Form1.EmpDBDataSet.Tables("empdata").Rows(Count).Item("phonelogin")

Please help if possible
 
G

Guest

ducky801,

Before you can use the array you need to redimension it to contain the
appropriate number of elements. Before the Do Until loop:

ReDim ArrLoginIDs(RowCount - 1)

Kerry Moorman
 
D

ducky801

Kerry said:
ducky801,

Before you can use the array you need to redimension it to contain the
appropriate number of elements. Before the Do Until loop:

ReDim ArrLoginIDs(RowCount - 1)

Kerry Moorman


Worked like a charm! Thanks so much for the help!

AR
 
L

Larry Lard

ducky801 said:
Hi all. When i run the code below i get a 'NullReferenceException was
unhandled' error. I am using VB 2005 Express.

I'm confused about this because, I have declared my array and i'm
trying to put things in it, so shouldn't it BE NULL until i start to
put things in it? I'm kind of new to this so hoepfully i'm just
overlooking something easy. I know my code is actually talking to the
DB (or dataset rather) because the messageboxes work. if i pull out
the buggy line, it will messagebox every login id in the table. Any
help that anybody can offer is greatly appreciated:


Module DynamicACSAuto
Public ArrLoginIDs() As Integer 'arra. holds phone logins. used
by createTBL()

Your confusion arises because arrays are not simple value types, but you
think they are :)

For simple value types such as Integer, when we say:

Dim a As Integer

we are saying 'create space for an Integer, and label this space 'a''.
Forever after, that space will be labeled a, and a will mean that space.

Howeer, for reference types - objects - of which arrays are an example,
when we say:

Dim a() As Integer

we are saying 'a is going to be used to *refer to* an array of
Integers'. We aren't actually creating such an array - just saying that
that is what a is going to be used for.

If we want to actually *create* an array for a to point to, we need to
say so:

a = New Integer(50) {}

This creates a new array of Integers and sets a to refer to it. If we
know what array we want at the time we declare a, we can make the
assignment at the same time:

Dim a() As Integer = New Integer(50) {}

or use the abbreviated syntax

Dim b(50) As Integer

However, I personally don't use the latter syntax, because it makes b
look like a value type. I also prefer to use the newer syntax for array
types, because it emphasizes that the 'array-ness' belongs to the type,
not the variable:

Dim a As Integer()

rather than

Dim a() As Integer
 
C

Chris Dunaway

Larry said:
However, I personally don't use the latter syntax, because it makes b
look like a value type. I also prefer to use the newer syntax for array
types, because it emphasizes that the 'array-ness' belongs to the type,
not the variable:

Dim a As Integer()

rather than

Dim a() As Integer

Another reason to use the first syntax above, is because that is the
way it is used when declaring functions and subs:

Public Sub PassIntArray(ints As Integer())
End Sub

Public Function ReturnIntArray() As Integer()
End Function
 

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