VB 2003 - Odd Bug in My Program

C

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
 
G

Guest

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
 
C

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

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
 
G

Guest

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


Confessor said:
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
 
C

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
 

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