473,499 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NullReferenceException when setting to array list

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?

Nov 21 '05 #1
9 1895
<sa******@gmail.com> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
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

Nov 21 '05 #3
<sa******@gmail.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4
On 2005-04-10, sa******@gmail.com <sa******@gmail.com> wrote:
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.
Nov 21 '05 #5
On 2005-04-10, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
<sa******@gmail.com> schrieb:
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 fact that this is hard to see argues
pretty strongly against using a For loop here, IMHO.

Sort(i, items)
Next i
///

Nov 21 '05 #6
>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

Nov 21 '05 #7
"David" <df*****@woofix.local.dom> schrieb:
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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
On 2005-04-11, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
"David" <df*****@woofix.local.dom> schrieb:
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.


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
Nov 21 '05 #9
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
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2227
by: Eric Diana | last post by:
I am trying to place an strings into an array of strings I am declaring as Dim Fields() As String I have another variable im doing the same thing with and it works. Public Shared Operations()...
5
2618
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
5
3042
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
1
4080
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
5
2615
by: Jeff Evans | last post by:
I have a custom composite control which has a validator for a textbox. The validator and textbox are declared in the class and created in the CreateChildControls() method Here is the code for the...
6
22195
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
5
6586
by: ducky801 | last post by:
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...
1
1661
by: musosdev | last post by:
Hi peeps, I'm suddenly getting a very strange error on one of my websites. It seems to be crashing within SaveViewState (?) but I can't tell if that the case, or if there is something else going...
1
17519
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5467
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.