473,624 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NullReferenceEx ception when setting to array list

Hi,

I'm having a problems with the following code. The line
"items.Add(coll ListViews(i))" gives me a NullReferenceEx ception 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.L ength
' 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(collL istViews(i))
End If
i += 1
End While

Return items
End Function

Any ideas?

Nov 21 '05 #1
9 1901
<sa******@gmail .com> schrieb:
I'm having a problems with the following code. The line
"items.Add(coll ListViews(i))" gives me a NullReferenceEx ception 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.L ength
' 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(collL istViews(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******@gmai l.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.it em(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.L ength
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(collL istViews(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
2240
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() As String This is the error I am getting
5
2632
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 windows service wich sends emails to customers at a specific time. My app retrieves cities and the city's associated time from a database, waits until the specified time for each city and sends out the appropriate emails. Below is some code that is...
5
3075
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 code available (I am running in Debug mode of course). What could be the cause of this? Can someone shed some light on this matter? Kind regards,
1
4085
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. ---------------------------------------------------------------------------- ------------------------------------------- Public Class TestClass Public NextSubIndex As Integer = 1 Public arrTestSubClass() As TestSubClass
5
2623
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 textbox, in CreateChildControls() searchBox = new TextBox(); searchBox.ID = "searchBox"; searchBox.EnableViewState = false; It is then added to a TableCell, which is added to a row, etc.
6
22207
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 Public Structure Attend Dim DaysTardy As Double
5
6592
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 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....
1
1673
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 on. The error occurs when I load a certain page with 2 lists, one shows a collection of contacts, and the other looks at which of those contacts are tied to a project. On load, I load the contacts list, and then loop through the project...
1
17541
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 before, lending proof to Einstein's statement: “Only two things are infinite, the universe and human stupidity ...”. Main Cause Dereferencing null. This is by far the more common cause of getting the exception. Reference types in both...
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.