Home All Groups Group Topic Archive Search About

Object reference not set to an instance of an object

Author
17 Jun 2006 10:38 AM
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

Author
17 Jun 2006 10:47 AM
Meelis
Dim t(1000) As Integer
For i = 0 To 1000
t(i) = i
Next


Meelis




Show quoteHide quote
"Bob" <s**@sdvsd.dc> wrote in message
news:%23Qq%23ApfkGHA.5036@TK2MSFTNGP04.phx.gbl...
> 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
>
>
Author
17 Jun 2006 10:57 AM
Bob
Thanks, but if i don't know in advance how many elements there will be ...

Show quoteHide quote
"Meelis" <m**@hot.ee> wrote in message
news:e%23voetfkGHA.3936@TK2MSFTNGP05.phx.gbl...
> Dim t(1000) As Integer
> For i = 0 To 1000
> t(i) = i
> Next
>
>
> Meelis
>
>
>
>
> "Bob" <s**@sdvsd.dc> wrote in message
> news:%23Qq%23ApfkGHA.5036@TK2MSFTNGP04.phx.gbl...
> > 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
> >
> >
>
>
Author
17 Jun 2006 11:03 AM
Meelis
Dim t() As Integer, i As Integer
For i = 0 To 1000
ReDim Preserve t(i)
t(i) = i
Next




Show quoteHide quote
"Bob" <s**@sdvsd.dc> wrote in message
news:OKBypzfkGHA.1456@TK2MSFTNGP04.phx.gbl...
> Thanks, but if i don't know in advance how many elements there will be ...
>
> "Meelis" <m**@hot.ee> wrote in message
> news:e%23voetfkGHA.3936@TK2MSFTNGP05.phx.gbl...
> > Dim t(1000) As Integer
> > For i = 0 To 1000
> > t(i) = i
> > Next
> >
> >
> > Meelis
> >
> >
> >
> >
> > "Bob" <s**@sdvsd.dc> wrote in message
> > news:%23Qq%23ApfkGHA.5036@TK2MSFTNGP04.phx.gbl...
> > > 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
> > >
> > >
> >
> >
>
>
Author
17 Jun 2006 11:05 AM
Bob
Thanks

Show quoteHide quote
"Meelis" <m**@hot.ee> wrote in message
news:Osw1d2fkGHA.4304@TK2MSFTNGP03.phx.gbl...
> Dim t() As Integer, i As Integer
> For i = 0 To 1000
> ReDim Preserve t(i)
> t(i) = i
> Next
>
>
>
>
> "Bob" <s**@sdvsd.dc> wrote in message
> news:OKBypzfkGHA.1456@TK2MSFTNGP04.phx.gbl...
> > Thanks, but if i don't know in advance how many elements there will be
....
> >
> > "Meelis" <m**@hot.ee> wrote in message
> > news:e%23voetfkGHA.3936@TK2MSFTNGP05.phx.gbl...
> > > Dim t(1000) As Integer
> > > For i = 0 To 1000
> > > t(i) = i
> > > Next
> > >
> > >
> > > Meelis
> > >
> > >
> > >
> > >
> > > "Bob" <s**@sdvsd.dc> wrote in message
> > > news:%23Qq%23ApfkGHA.5036@TK2MSFTNGP04.phx.gbl...
> > > > 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
> > > >
> > > >
> > >
> > >
> >
> >
>
>
Author
17 Jun 2006 2:12 PM
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


Show quoteHide quote
"Bob" <s**@sdvsd.dc> wrote in message
news:%23Qq%23ApfkGHA.5036@TK2MSFTNGP04.phx.gbl...
| 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
|
|