NullReferenceException when setting to array list

S

sas.dean

Hi,

I'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?
 
H

Herfried K. Wagner [MVP]

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

How do you call the 'sort' function? BTW: There is no need to use a
function and 'Return...' in this case. The arraylist passed to the method
will be updated when accessing 'items'.
 
S

sas.dean

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
 
H

Herfried K. Wagner [MVP]

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

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
Sort(i, items)
Next i
///
 
D

David

I call the sort function by using
While i <> 4
questions = sort(i, items)
i += 1
End While

Since you're dying on the line the references Items, the real question
is where does the items arraylist get initialized.
 
D

David

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 fact that this is hard to see argues
pretty strongly against using a For loop here, IMHO.
 
S

sas.dean

That introduces an off-by-one error. The first loop runs 4 times,
your
replacement runs 5 times. The fact that this is hard to see argues
pretty strongly against using a For loop here, IMHO.

Yeah i picked that up and corrected it, still using a for loop though.

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
 
H

Herfried K. Wagner [MVP]

David said:
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.
 
D

David

ACK, maybe I was too busy to fully concentrate on what I type when writing
the reply.


I still think that 'For...To' is the better solution in this case, at least
from what we know about it.

Given the complete context of the final solution, incrementing an
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
 
C

Cor Ligthert

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
 

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