|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
NullReferenceException when setting to array listI'm having a problems with the following code. The line "items.Add(collListViews(i))" gives me a NullReferenceException and I don't really know why.. Private Function sort(ByVal priority As Integer, ByVal items As ArrayList) Dim i As Integer = 0 ' while all listviewitems haven't been looped through ' colllistviews is an array of listviewitems 100 big but only has data in 0-4 While i < collListViews.Length ' if priorities match If collListViews(i).SubItems.Item(2).Text = priority Then ' add that item to test arraylist ' get null reference exception here items.Add(collListViews(i)) End If i += 1 End While Return items End Function Any ideas? <sas.d***@gmail.com> schrieb:
Show quoteHide quote > I'm having a problems with the following code. The line How do you call the 'sort' function? BTW: There is no need to use a > "items.Add(collListViews(i))" gives me a NullReferenceException and I > don't really know why.. > > > > Private Function sort(ByVal priority As Integer, ByVal items As > ArrayList) > Dim i As Integer = 0 > ' while all listviewitems haven't been looped through > ' colllistviews is an array of listviewitems 100 big but only has data > in 0-4 > While i < collListViews.Length > ' if priorities match > > If collListViews(i).SubItems.Item(2).Text = priority Then > ' add that item to test arraylist > ' get null reference exception here > items.Add(collListViews(i)) > End If > i += 1 > End While > > Return items > End Function function and 'Return...' in this case. The arraylist passed to the method will be updated when accessing 'items'. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> I call the sort function by using
While i <> 4 questions = sort(i, items) i += 1 End While I don't need "function and return"? So sort should be a sub and called like: While i <> 4 sort(i, items) i += 1 End While <sas.d***@gmail.com> schrieb:
>I call the sort function by using Yes, however, I would replace the 'While' loop with a 'For...To' loop:> While i <> 4 > questions = sort(i, items) > i += 1 > End While > > I don't need "function and return"? So sort should be a sub and called > like: > > While i <> 4 > sort(i, items) > i += 1 > End While \\\ Dim items As New ArrayList() For i As Integer = 0 To 4 Sort(i, items) Next i /// -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> On 2005-04-10, Herfried K. Wagner [MVP] <hirf-spam-me-here@gmx.at> wrote:
Show quoteHide quote ><sas.d***@gmail.com> schrieb: That introduces an off-by-one error. The first loop runs 4 times, your>> I don't need "function and return"? So sort should be a sub and called >> like: >> >> While i <> 4 >> sort(i, items) >> i += 1 >> End While > > Yes, however, I would replace the 'While' loop with a 'For...To' loop: > > \\\ > Dim items As New ArrayList() > For i As Integer = 0 To 4 replacement runs 5 times. The fact that this is hard to see argues pretty strongly against using a For loop here, IMHO. Show quoteHide quote > Sort(i, items) > Next i > /// >
Show quote
Hide quote
"David" <dfos***@woofix.local.dom> schrieb: ACK, maybe I was too busy to fully concentrate on what I type when writing >>> I don't need "function and return"? So sort should be a sub and called >>> like: >>> >>> While i <> 4 >>> sort(i, items) >>> i += 1 >>> End While >> >> Yes, however, I would replace the 'While' loop with a 'For...To' loop: >> >> \\\ >> Dim items As New ArrayList() >> For i As Integer = 0 To 4 > > That introduces an off-by-one error. The first loop runs 4 times, your > replacement runs 5 times. the reply. > The fact that this is hard to see argues I still think that 'For...To' is the better solution in this case, at least > pretty strongly against using a For loop here, IMHO. from what we know about it. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> On 2005-04-11, Herfried K. Wagner [MVP] <hirf-spam-me-here@gmx.at> wrote:
Show quoteHide quote > "David" <dfos***@woofix.local.dom> schrieb: Given the complete context of the final solution, incrementing an>>> Dim items As New ArrayList() >>> For i As Integer = 0 To 4 >> >> That introduces an off-by-one error. The first loop runs 4 times, your >> replacement runs 5 times. > > ACK, maybe I was too busy to fully concentrate on what I type when writing > the reply. > >> The fact that this is hard to see argues >> pretty strongly against using a For loop here, IMHO. > > I still think that 'For...To' is the better solution in this case, at least > from what we know about it. counter over each index of a collection, I'd agree. But I'm curious what you'd really choose for the problem as presented originally: execute a series of commands some specific number of times. In Mcconnellesque detail, say we have a retry count count... For i = 0 to MaxAttempts is wrong, as we've seen. For i = 1 to MaxAttempts just feels wrong to me. my mind is zero-based, I can't help it. And if I ever do need the index, I haven't got it. For i = 0 to MaxAttempts - 1 That's not so bad, and I pretty much consider it a standard VB.net idiom. However, I was very uncomfortable with it when I first started on VB.Net. [Do] While i < MaxAttempts ... do stuff i += 1 Loop This feels pretty good to me also, although separating the increment might bother some. I lean toward #3, but the fact that #1 is so error-prone and so similar to #3 makes me worry about it a bit. Of course, it also occurs to me that I very seldom have loops like this. It seems I'm usually dealing with the length or count of something. David David,
You will typical Cobol users see forever use the For. It is one of the strongest parts from Cobol the construction is the perFORm. And has been there forever. In Cobol that instruction is almost from the beginning a really very strong part, you are not only able to increment one value in the loop, however a lot, and even based on evaluations, with what you make endless dimensioned table handling possible in just one instruction. It is probably the first thing a really good Cobol programmer misses in any other language. Luckily is the nested index FOR a (not so strong) replacement for that. Therefore you will see programmers without Cobol background in the beginning probably use the do(while) because they did not see that powerfull instruction as the indexed for is and do table handling completly with a lot of code. Maybe you know this, when not than maybe it helps you to think it over. Cor On 2005-04-10, sas.d***@gmail.com <sas.d***@gmail.com> wrote:
> I call the sort function by using Since you're dying on the line the references Items, the real question> While i <> 4 > questions = sort(i, items) > i += 1 > End While is where does the items arraylist get initialized. >That introduces an off-by-one error. The first loop runs 4 times, Yeah i picked that up and corrected it, still using a for loop though.your >replacement runs 5 times. The fact that this is hard to see argues >pretty strongly against using a For loop here, IMHO. I've just realised what my problem is, when my code gets to an array item that "is nothing" it dies, as it's trying to access ArrayOfListView(i).subitems.item(2).text. Of course if the array item is nothing then you subitems.item(2).text isn't a member of it. So i just added in an "If ArrayOfListView(i) Is Nothing Then...." it works fine now :) Just for reference my code looks like this: Sub sort(ByVal priority As Integer, ByVal items As ArrayList) Dim i As Integer = 0 ' colllistviews is an array of listview items While i < collListViews.Length If collListViews(i) Is Nothing Then Exit While Else ' if priorities match If collListViews(i).SubItems.Item(2).Text = priority Then ' add that item to items arraylist items.Add(collListViews(i)) End If End If i += 1 End While End Sub Many thanks Herfried and David
Visual Basic.net
Access vs SQL How can I let the internet explorer start to navigate in the same windwo? How to catch a right-click TAB (key) to next control instead of next column (in datagrid) Writing to Web Server Naming convention for objects Re: Freeze Column in DataGrid why does my app.productionversion not auto increment? Insert statement for the DateTime field |
|||||||||||||||||||||||