|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
A little help with an array & 'NullReferenceException was unhandled' ??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 ducky801 wrote:
Show quoteHide quote > Hi all. When i run the code below i get a 'NullReferenceException was Something got trimmed in the post above. the buggy line reads:> 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 (without wrapping) ArrLoginIDs(Count - 1) = Form1.EmpDBDataSet.Tables("empdata").Rows(Count).Item("phonelogin") Please help if possible 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 Show quoteHide quote "ducky801" wrote: > > ducky801 wrote: > > 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 > > Kerry Moorman wrote:
> ducky801, Worked like a charm! Thanks so much for the help!> > 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 > AR ducky801 wrote:
Show quoteHide quote > Hi all. When i run the code below i get a 'NullReferenceException was Your confusion arises because arrays are not simple value types, but you > 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() 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 -- Larry Lard larryl***@googlemail.com The address is real, but unread - please reply to the group For VB and C# questions - tell us which version Larry Lard wrote:
> However, I personally don't use the latter syntax, because it makes b Another reason to use the first syntax above, is because that is the> 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 > way it is used when declaring functions and subs: Public Sub PassIntArray(ints As Integer()) End Sub Public Function ReturnIntArray() As Integer() End Function |
|||||||||||||||||||||||