Object reference not set to an instance of an object

B

Bob

Hello,

I'm starting with vb.net and i get this error:
Object reference not set to an instance of an object

Dim t() As Integer
For i = 0 To 1000
t(i) = i
Next

Thanks
Bob
 
M

Meelis

Dim t() As Integer, i As Integer
For i = 0 To 1000
ReDim Preserve t(i)
t(i) = i
Next
 
J

Jay B. Harlow [MVP - Outlook]

Bob,
In addition Meelis' sample.

Consider using an ArrayList (.NET 1.x) or List(Of T) (.NET 2.0) instead.

Both are array like structures that dynamic grow "intelligently". They both
allocate a larger then needed buffer & only reallocate when the buffer in
full. Which is significantly more GC friendly then resizing the array on
each iteration of the loop.

' .NET 1.x
Dim t As ArrayList
| For i = 0 To 1000
| t(i) = i
| Next

' .NET 2.x
Dim t As List(Of Integer)
| For i = 0 To 1000
| t(i) = i
| Next

The ArrayList actually holds a list of Objects, so it is not very GC
friendly, nor type safe friendly, plus it has overridable methods which can
be a minor performance issue.

The List(Of Integer) holds a list of Integers, so it is very GC friendly,
fully type safe, plus avoids overriable methods eliminating that possible
performance issue...

FWIW: List(Of T) is a generic class, it is a strongly typed list of what
ever type you give for T. For example:

List(Of Integer) = a strongly typed list of integers
List(Of Double) = a strongly typed list of doubles
List(Of Customer) = a strongly typed list of Customer objects


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello,
|
| I'm starting with vb.net and i get this error:
| Object reference not set to an instance of an object
|
| Dim t() As Integer
| For i = 0 To 1000
| t(i) = i
| Next
|
| Thanks
| Bob
|
|
 

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