Home All Groups Group Topic Archive Search About

VB 2003 - Odd Bug in My Program

Author
6 Jan 2006 10:55 PM
Confessor
*Confessor nods*

Okay, I've got a hell of a lead on how to read/write to Random Access
files, but there's a small problem that's stopping me short of sussing
out that whole thing.

The "arrowed" line of code (as well as the two lines following, if I skip
the first/second by commenting) pops the error 'Object reference not set
to an instance of an object,' no matter what I input into the textbox.

The problem can't be (MaxPoint), since Label5.Text confirms that it's
value is 1 prior to the execution of Button1_Click, and since no code (as
of yet) even *creates* points.dat, there's no situation in which its
value can be anything other than 1 following Form1_Load.

(Note that I've used ... to excise boring, simple parts in the following
code.)

    Structure PointTemplate
        Dim Latitude As Single
        Dim Longitude As Single
        Dim Elevation As Integer
    End Structure
    Dim MaxPoint As Integer
    Dim Point() As PointTemplate

    Private Sub Form1_Load(...) Handles MyBase.Load
        If IO.File.Exists("C:\...\points.dat") Then
        Else
            MaxPoint = 1
        End If
        Label5.Text = MaxPoint
    End Sub

    Private Sub Button1_Click(...) Handles Button1.Click
--->    Point(MaxPoint).Latitude = TextBox2.Text    <---
        Point(MaxPoint).Longitude = TextBox3.Text
        Point(MaxPoint).Elevation = TextBox4.Text
        MaxPoint = MaxPoint + 1
    End Sub

Thanks in Advance,
The Confessor

Author
6 Jan 2006 11:26 PM
Kerry Moorman
Confessor,

I don't see where you have dimensioned your Point array to have any elements.

So you can't refer to Point (MaxPoint), where MaxPoint is 1, since there is
no element 1 in the Point array.

Kerry Moorman


Show quoteHide quote
"Confessor" wrote:

> *Confessor nods*
>
> Okay, I've got a hell of a lead on how to read/write to Random Access
> files, but there's a small problem that's stopping me short of sussing
> out that whole thing.
>
> The "arrowed" line of code (as well as the two lines following, if I skip
> the first/second by commenting) pops the error 'Object reference not set
> to an instance of an object,' no matter what I input into the textbox.
>
> The problem can't be (MaxPoint), since Label5.Text confirms that it's
> value is 1 prior to the execution of Button1_Click, and since no code (as
> of yet) even *creates* points.dat, there's no situation in which its
> value can be anything other than 1 following Form1_Load.
>
> (Note that I've used ... to excise boring, simple parts in the following
> code.)
>
>     Structure PointTemplate
>         Dim Latitude As Single
>         Dim Longitude As Single
>         Dim Elevation As Integer
>     End Structure
>     Dim MaxPoint As Integer
>     Dim Point() As PointTemplate
>
>     Private Sub Form1_Load(...) Handles MyBase.Load
>         If IO.File.Exists("C:\...\points.dat") Then
>         Else
>             MaxPoint = 1
>         End If
>         Label5.Text = MaxPoint
>     End Sub
>
>     Private Sub Button1_Click(...) Handles Button1.Click
> --->    Point(MaxPoint).Latitude = TextBox2.Text    <---
>         Point(MaxPoint).Longitude = TextBox3.Text
>         Point(MaxPoint).Elevation = TextBox4.Text
>         MaxPoint = MaxPoint + 1
>     End Sub
>
> Thanks in Advance,
> The Confessor
>
Author
7 Jan 2006 12:17 AM
Confessor
"=?Utf-8?B?S2VycnkgTW9vcm1hbg==?="
<KerryMoor***@discussions.microsoft.com> wrote in
news:0E485F23-FAC4-4F24-B676-193F4668B974@microsoft.com:

> I don't see where you have dimensioned your Point array to have any
> elements.
>
> So you can't refer to Point (MaxPoint), where MaxPoint is 1, since
> there is no element 1 in the Point array.
>
> Kerry Moorman
>

So,

---> Dim Point([any frickin' number]) As PointTemplate <---

will do it, eh? Makes sense. Don't recall having this problem in a previous
project, but I'd forgotten that I'd specifically dimensioned that
particular array to (85)

Rather discouraging, as I was planning on having a variable "cap" for the
array, such that the upper limit would always be equal to the current
MaxPoint.

And if I set Point() to an insane cap like 2000000, I'll have to institute
a check prior to each write for null values of Point(T).[Structure
Members], just to minimize storage.

Hmm... It's enough to make me give a shot at understanding Serializeable
Classes

Thanks for your help,

The Confessor
Author
7 Jan 2006 12:59 AM
Kerry Moorman
Confessor,

Once you know the array size that you need you can use the REDIM command to
create an array of the appropriate size. Or you can use REDIM PRESERVE to
increase the array size as needed, while preserving the current elements.

Kerry Moorman


Show quoteHide quote
"Confessor" wrote:

> "=?Utf-8?B?S2VycnkgTW9vcm1hbg==?="
> <KerryMoor***@discussions.microsoft.com> wrote in
> news:0E485F23-FAC4-4F24-B676-193F4668B974@microsoft.com:
>
> > I don't see where you have dimensioned your Point array to have any
> > elements.
> >
> > So you can't refer to Point (MaxPoint), where MaxPoint is 1, since
> > there is no element 1 in the Point array.
> >
> > Kerry Moorman
> >
>
> So,
>
> ---> Dim Point([any frickin' number]) As PointTemplate <---
>
> will do it, eh? Makes sense. Don't recall having this problem in a previous
> project, but I'd forgotten that I'd specifically dimensioned that
> particular array to (85)
>
> Rather discouraging, as I was planning on having a variable "cap" for the
> array, such that the upper limit would always be equal to the current
> MaxPoint.
>
> And if I set Point() to an insane cap like 2000000, I'll have to institute
> a check prior to each write for null values of Point(T).[Structure
> Members], just to minimize storage.
>
> Hmm... It's enough to make me give a shot at understanding Serializeable
> Classes
>
> Thanks for your help,
>
> The Confessor
>
Author
7 Jan 2006 2:17 AM
Confessor
Mr. Moorman

I now have workable, if not exactly elegant, code... with a 38 Kilobyte
points.dat file that I can open and save at will to prove it.

Thank You,
The Confessor


"=?Utf-8?B?S2VycnkgTW9vcm1hbg==?="
<KerryMoor***@discussions.microsoft.com> wrote in
Show quoteHide quote
news:EA629DDE-A96D-4548-AF8B-E6DB0F8E36DD@microsoft.com:

> Confessor,
>
> Once you know the array size that you need you can use the REDIM
> command to create an array of the appropriate size. Or you can use
> REDIM PRESERVE to increase the array size as needed, while preserving
> the current elements.
>
> Kerry Moorman